Django official tutorial for the absolute beginner, absolutely failed!

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

Not that level of failure indeed. I just completed the 4 part tutorial from djangoproject.com, my administration app works fine and my entry point url (/polls/) works well, with the exception that I get this http response:

No polls are available.

Even if the database has one registry. Entering with the admin app, the entry shows up the way it should be.

At the end of the tutorial, you change all your hard-coded views by replacing it for generic views on your URLconf. It's supossed that after all the modifications your urls.py ends up like this:

from django.conf.urls.defaults import * from mysite.polls.models import Poll  info_dict = {     'queryset': Poll.objects.all(), }  urlpatterns = patterns('',     (r'^$', 'django.views.generic.list_detail.object_list', info_dict),     (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),     url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html'), 'poll_results'),     (r'^(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'), ) 

Using these generic views, It'll be pointless to copy/paste my views.py file, I'll only mention that there's just a vote function (since django generic views do all the magic). My supposition is that the urls.py file needs some tweak, or is wrong at something In order to send that "No polls available." output at /polls/ url. My poll_list.html file looks like this:

{% if latest_poll_list %}     <ul>     {% for poll in latest_poll_list %}         <li>{{ poll.question }}</li>     {% endfor %}     </ul> {% else %}     <p>No polls are available.</p> {% endif %} 

It evals latest_poll_list to false, and that's why the else block is executed.

Can you give me a hand at this? (I searched at stackoverflow for duplicate question's, and even at google for this issue, but I couldn't find anything). Why do I get this message when I enter at http://127.0.0.1:8000/polls?

回答1:

You overlooked this paragraph in the 4. part of the tutorial:

In previous parts of the tutorial, the templates have been provided with a context that contains the poll and latest_poll_list context variables. However, the generic views provide the variables object and object_list as context. Therefore, you need to change your templates to match the new context variables. Go through your templates, and modify any reference to latest_poll_list to object_list, and change any reference to poll to object.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!