[英]Django: TypeError at /1/ context must be a dict rather than QuerySet
this is my first Post on this Site. 这是我在本网站上的第一篇文章。 I am learning Django and Python right now and trying to create a Quiztool. 我现在正在学习Django和Python,并尝试创建Quiztool。 I have hughe problems with creating my views and its hard for me to understand how to refine the data in a Queryset. 我在创建视图时遇到了麻烦,很难理解如何优化Queryset中的数据。 In my Detail View I am raising this error: 在我的详细信息视图中,我提出了这个错误:
TypeError at /1/ / 1 /处的TypeError
context must be a dict rather than QuerySet. 上下文必须是字典而不是QuerySet。
Request Method: GET Request URL: http://192.168.188.146:8080/1/ Django Version: 2.0.1 Exception Type: TypeError Exception Value: 请求方法:GET请求URL: http : //192.168.188.146 : 8080/1/ Django版本:2.0.1异常类型:TypeError异常值:
context must be a dict rather than QuerySet. 上下文必须是字典而不是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: 异常位置:make_context中第274行的/home/flo/Django2.0/lib/python3.5/site-packages/django/template/context.py Python可执行文件:/home/flo/Django2.0/bin/python Python版本:3.5.3 Python路径:
['/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'] ['/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 服务器时间:2018年3月1日,星期四11:00:35 +0000
I know I have to put the Queryset into a Dictonary but i dont know how to do this. 我知道我必须将Queryset放入字典中,但是我不知道该怎么做。
Here is my views.py: 这是我的views.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)
And here the detail.html: 这是detail.html:
{% if question %}
<ul>
{% for x in question %}
<li>{{ x.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No questions are available.</p>
{% endif %}
If u need further informations to help me just ask. 如果您需要进一步的信息来帮助我,请询问。
Many thanks in advance and my Regards flotzen 在此先多谢,我的问候致谢
You're returning question
rather then the dic question_dict
in here: 您返回的是question
而不是此处的dic question_dict
:
return render(request, 'fragen/detail.html', question)
it should be 它应该是
return render(request, 'fragen/detail.html', question_dict)
将您需要的所有内容放入“ question_dict”字典中的模板中,然后通过如下所示的渲染:
return render(request, 'fragen/detail.html', context=question_dict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.