gpt4 book ai didi

python - Django:TypeError at/1/context 必须是字典而不是 QuerySet

转载 作者:行者123 更新时间:2023-11-30 22:19:40 24 4
gpt4 key购买 nike

这是我在此网站上的第一篇文章。我现在正在学习 Django 和 Python,并尝试创建一个 Quiztool。我在创建 View 时遇到了巨大的问题,并且我很难理解如何优化查询集中的数据。在我的详细 View 中,我提出了此错误:

TypeError at /1/

context must be a dict rather than QuerySet.

Request Method: GET Request URL: http://192.168.188.146:8080/1/ Django Version: 2.0.1 Exception Type: TypeError Exception Value:

context must be a dict rather than QuerySet.

Exception Location: /home/flo/Django2.0/lib/python3.5/site-packages/django/template/context.py in make_context, line 274 Python Executable: /home/flo/Django2.0/bin/python Python Version: 3.5.3 Python Path:

['/home/flo/Django2.0/quiztool', '/home/flo/Django2.0/lib/python35.zip', '/home/flo/Django2.0/lib/python3.5', '/home/flo/Django2.0/lib/python3.5/plat-x86_64-linux-gnu', '/home/flo/Django2.0/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/home/flo/Django2.0/lib/python3.5/site-packages']

Server time: Thu, 1 Mar 2018 11:00:35 +0000

我知道我必须将查询集放入字典中,但我不知道该怎么做。

这是我的观点.py:

def index(request):
latest_survey_list = Survey.objects.order_by('survey_id')[:5]

context = {
'latest_survey_list': latest_survey_list
}
return render(request, 'fragen/index.html', context)

def detail(request, survey_id):

question = Survey.objects.get(pk=survey_id).question.all().values()
question_dict = {
'question': question
}

return render(request, 'fragen/detail.html', question)

这里是detail.html:

{% if question %}
<ul>
{% for x in question %}
<li>{{ x.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No questions are available.</p>
{% endif %}

如果您需要更多信息来帮助我,请询问。

提前非常感谢,我的问候弗洛岑

最佳答案

您返回的是 question,而不是此处的 dic question_dict:

return render(request, 'fragen/detail.html', question)

应该是

return render(request, 'fragen/detail.html', question_dict)

关于python - Django:TypeError at/1/context 必须是字典而不是 QuerySet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49048494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com