Python 无法将关键字“pub_u2;”解析为views.py中的字段
在下面的教程中, 我的操作与教程中的指导完全相同,在polls/templates/polls/index.html中创建模板文件:Python 无法将关键字“pub_u2;”解析为views.py中的字段,python,django,Python,Django,在下面的教程中, 我的操作与教程中的指导完全相同,在polls/templates/polls/index.html中创建模板文件: polls/templates/polls/index.html¶ {% if latest_question_list %} <ul> {% for question in latest_question_list %} <li><a href="/polls/{{ question.id }}/"
polls/templates/polls/index.html¶
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
当我启动服务器并访问URL:时,出现以下错误:
FieldError at /polls/
Cannot resolve keyword 'pub_' into field. Choices are: choice, id, pub_date, question_text
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 2.1.7
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'pub_' into field. Choices are: choice, id, pub_date, question_text
Exception Location: /home/martin/anaconda3/envs/web/lib/python3.7/site-packages/django/db/models/sql/query.py in names_to_path, line 1389
Python Executable: /home/martin/anaconda3/envs/web/bin/python
Python Version: 3.7.2
Python Path:
['/home/martin/nlp/web/web',
'/home/martin/anaconda3/envs/web/lib/python37.zip',
'/home/martin/anaconda3/envs/web/lib/python3.7',
'/home/martin/anaconda3/envs/web/lib/python3.7/lib-dynload',
'/home/martin/anaconda3/envs/web/lib/python3.7/site-packages']
Server time: Sun, 3 Mar 2019 09:20:08 +0000
Error during template rendering
In template /home/martin/nlp/web/web/polls/templates/polls/index.html, error at line 1
Cannot resolve keyword 'pub_' into field. Choices are: choice, id, pub_date, question_text
1 {% if latest_question_list %}
2 <ul>
3 {% for question in latest_question_list %}
4 <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
5 {% endfor %}
6 </ul>
7 {% else %}
8 <p>No polls are available.</p>
9 {% endif %}
10
我在应用程序的终端运行这一行,没有发现任何问题。但是,当我在服务器上运行应用程序时,我总是遇到这样一个恼人的问题。您的代码中是否有打字错误: 我猜你是这样写的: 问题.对象.按'-pub_date'[:5]排序 这意味着所有问题都按名为“pub_u”的字段降序排列,因为“-pub_udate”[:5]是“-pub_u” 您要编写以下代码: 问题.对象.按'-pub_date'[:5]排序
这意味着最新的五个问题。也许你是按“-pub_date”[:5]写订单,而不是按“-pub_date”[:5]写订单?有什么区别?正是sameLook仔细的。括号后或字符串后的括号:pub_date'[vs pub_date'[。完全正确!谢谢,我将其作为解决方案发布。
FieldError at /polls/
Cannot resolve keyword 'pub_' into field. Choices are: choice, id, pub_date, question_text
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 2.1.7
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'pub_' into field. Choices are: choice, id, pub_date, question_text
Exception Location: /home/martin/anaconda3/envs/web/lib/python3.7/site-packages/django/db/models/sql/query.py in names_to_path, line 1389
Python Executable: /home/martin/anaconda3/envs/web/bin/python
Python Version: 3.7.2
Python Path:
['/home/martin/nlp/web/web',
'/home/martin/anaconda3/envs/web/lib/python37.zip',
'/home/martin/anaconda3/envs/web/lib/python3.7',
'/home/martin/anaconda3/envs/web/lib/python3.7/lib-dynload',
'/home/martin/anaconda3/envs/web/lib/python3.7/site-packages']
Server time: Sun, 3 Mar 2019 09:20:08 +0000
Error during template rendering
In template /home/martin/nlp/web/web/polls/templates/polls/index.html, error at line 1
Cannot resolve keyword 'pub_' into field. Choices are: choice, id, pub_date, question_text
1 {% if latest_question_list %}
2 <ul>
3 {% for question in latest_question_list %}
4 <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
5 {% endfor %}
6 </ul>
7 {% else %}
8 <p>No polls are available.</p>
9 {% endif %}
10
latest_question_list = Question.objects.order_by('-pub_date')[:5]