Django官方教程为绝对的初学者,绝对失败! [英] Django official tutorial for the absolute beginner, absolutely failed!

查看:173
本文介绍了Django官方教程为绝对的初学者,绝对失败!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是那个失败的程度。我刚刚完成了djangoproject.com的4部分教程,我的管理应用程序工作正常,我的入口点url(/ polls /)工作得很好,除了我得到这个http响应:

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:

没有轮询可用。

即使数据库有一个注册表。使用管理员应用程序输入该条目。

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

在本教程结尾处,您可以更改所有硬编码视图,替换为你的URLconf的一般视图。很遗憾,所有修改后,您的urls.py最终都是这样的:

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'),
)

使用这些通用视图,复制/粘贴我的views.py文件是毫无意义的,我只会提到只有一个投票功能(因为django通用视图做一个魔法)。我的想法是,urls.py文件需要一些调整,或者是错误的东西为了发送没有轮询可用。输出在/ polls / url。我的poll_list.html文件如下所示:

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 %}

它将latest_poll_list设为false,这就是其他块执行。

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

你能给我一个手吗? (我在stackoverflow中搜索重复的问题,甚至在google这个问题,但我找不到任何东西)。 当我输入 http://127.0.0.1:8000/polls

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?

推荐答案

您在本教程的4.部分忽略了这一段:

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


在本教程的前面部分,模板已经提供了一个包含轮询的上下文, latest_poll_list 上下文变量。但是,通用视图提供变量 object object_list 作为上下文。因此,您需要更改模板以匹配新的上下文变量。通过您的模板,并修改任何对 latest_poll_list 的引用到 object_list ,并将任何引用更改为 poll to object

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.

这篇关于Django官方教程为绝对的初学者,绝对失败!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆