【问题标题】:Context must be a dict rather than list rendering template上下文必须是字典而不是列表渲染模板
【发布时间】:2021-04-25 00:39:17
【问题描述】:

我正在用 django 做一个查询集。现在我在渲染模板时遇到了一点问题。它重新调整了此错误“上下文必须是字典而不是列表”。我知道上下文应该作为字典返回,我怀疑在context['cursos'] 行中。任何人都可以确认我的嫌疑人或给我一些解决方案吗?提前致谢

class ListCursos( TemplateView):
    model1 = User
    model2 = Course
    template_name = 'plantillas/miscursos.html'

    def get_context_data(self, *args, **kwargs):
        context = super(ListCursos, self).get_context_data(**kwargs)
        rels = CourseRelUser.objects.filter(user_id=1)
        courses_id=[]
        for rel in rels:
            courses_id.append(rel.c_id)
            return courses_id

        context['cursos'] = Course.objects.filter(id__in=courses_id)
        return context
        

这是完整的回溯错误:https://i.stack.imgur.com/oVseL.png

    Internal Server Error: /core/miscursos/
Traceback (most recent call last):
  File "C:\Users\juand\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\juand\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Users\juand\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Users\juand\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "C:\Users\juand\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py", line 59, in render
    context = make_context(context, request, autoescape=self.backend.engine.autoescape)
  File "C:\Users\juand\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\context.py", line 268, in make_context
    raise TypeError('context must be a dict rather than %s.' % context.__class__.__name__)
TypeError: context must be a dict rather than list.
[20/Jan/2021 11:08:22] "GET /core/miscursos/ HTTP/1.1" 500 76063

【问题讨论】:

  • 请包含完整的堆栈跟踪(错误),因为从您包含的内容来看,它似乎是从超级获取上下文,然后像字典一样使用它并返回它,因此错误必须在其他地方。
  • @markwalker_ 这是你要求的吗?
  • 图像通常很有帮助。您可以将控制台(我们在您的终端中运行的服务器)中出现的错误详细信息复制并粘贴到此处吗?
  • 你有两个 return 语句,所以当你到达 return courses_id 时,你的函数结束并且函数的其余部分没有被执行,所以你只返回 course_id,这是一个列表。此外,您正在过滤一个 id = 1 的特定 CourseRelUser,但我怀疑您只想获得一个 id = 1 的用户。我不知道您的模型的结构,但您可能可以有一个 .filter( ) 并指定您只需要 id = 1 的用户的结果
  • @markwalker_ 解决方案来自 Eduardo 第一次返回不是必需的。谢谢各位

标签: python python-3.x django django-templates


【解决方案1】:

好的,所以听从 Eduardo 的意见。我证明从 for 循环中删除 return courses_id 并且它工作正常

class ListCursos( TemplateView):
model1 = User
model2 = Course
template_name = 'plantillas/miscursos.html'

def get_context_data(self, *args, **kwargs):
    context = super(ListCursos, self).get_context_data(**kwargs)
    rels = CourseRelUser.objects.filter(user_id=1)
    courses_id=[]
    for rel in rels:
        courses_id.append(rel.c_id)

    context['cursos'] = Course.objects.filter(id__in=courses_id)
    return context

【讨论】:

  • 很高兴知道它有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多