姜戈教程。 404 关于通用视图

发布于 2024-12-05 19:41:29 字数 1356 浏览 0 评论 0原文

更新:使用 Dreamhost 提供的 Django 1.2.1 和 Python 2.5.2。

我在 Django 教程的最后部分遇到问题,其中 urls.py 更改为使用通用视图。更改代码后,页面上出现 404,甚至索引也停止工作。

我已经检查了所有模板,看看这是否是问题所在,但我删除了 poll 的任何实例并将其替换为 object。我还附上了索引/对象列表的模板。

我在 Dreamhost 上运行它,并且我使用视图设置的静态 url 工作得很好。

urls.py

from brsplash.models import Poll
from django.conf.urls.defaults import *
from django.contrib import admin
from django.views.generic import *
admin.autodiscover()

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='brsplash/results.html'), 'poll_results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'brsplash.views.vote'),
)    

urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),

poll_list.html

    {% if object_list %}
    <ul>
    {% for object in object_list %}
        <li><a href="{{ object.id }}/">{{ object.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available</p>
{% endif %}

Update: Using Django 1.2.1 and Python 2.5.2 as offered by Dreamhost.

I'm having issues with the last part of the Django tutorial where the urls.py is changed to use generic views. After I change the code I get 404's on the pages and even the index stops working.

I have gone over all of my templates to see if that was the issue but I removed any instance of poll and replaced it with object. I have also attached the template for the index/object_list.

I am running this on Dreamhost and the static urls I set with views worked fine.

urls.py

from brsplash.models import Poll
from django.conf.urls.defaults import *
from django.contrib import admin
from django.views.generic import *
admin.autodiscover()

info_dict = {
    'queryset': Poll.objects.all(),
}
urlpatterns = patterns('',
    (r'^

poll_list.html

    {% if object_list %}
    <ul>
    {% for object in object_list %}
        <li><a href="{{ object.id }}/">{{ object.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available</p>
{% endif %}
, 'django.views.generic.list_detail.object_list', info_dict), (r'^(?P<object_id>\d+)/

poll_list.html


, 'django.views.generic.list_detail.object_detail',     info_dict),
    url(r'^(?P<object_id>\d+)/results/

poll_list.html


, 'django.views.generic.list_detail.object_detail', dict(info_dict,     template_name='brsplash/results.html'), 'poll_results'),
    (r'^(?P<poll_id>\d+)/vote/

poll_list.html


, 'brsplash.views.vote'),
)    

urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),

poll_list.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2024-12-12 19:41:29

Django 1.3 引入了基于类的通用视图,它将取代这种基于函数的方法(请参阅 文档页面)所以也许最好使用它们。

使用基于类的方法,您的新详细信息页面 url 将如下所示:

from brsplash.models import Poll
...
from django.views.generic import ListView

urlpatterns = {'',
    url(r'^

此方法可以在 教程的第 4 部分

注意:我倾向于不将 template_name 参数传递给 as_view 因为,如文档中所述:

ListView 通用视图使用名为 /_list.html

, ListView.as_view(model=Poll)), ... }

此方法可以在 教程的第 4 部分

注意:我倾向于不将 template_name 参数传递给 as_view 因为,如文档中所述:

ListView 通用视图使用名为/_list.html

Django 1.3 introduced class-based generic views which will replace this function-based approach (see the note at the top of the documentation page) so perhaps it's best to use them instead.

With the class-based approach, your new detail-page url would look something like this:

from brsplash.models import Poll
...
from django.views.generic import ListView

urlpatterns = {'',
    url(r'^

This approach can be found in part 4 of the tutorial.

N.B.: I tend not to pass the template_name argument to as_view because, as stated in the docs:

ListView generic view uses a default template called <app name>/<model name>_list.html

, ListView.as_view(model=Poll)), ... }

This approach can be found in part 4 of the tutorial.

N.B.: I tend not to pass the template_name argument to as_view because, as stated in the docs:

ListView generic view uses a default template called <app name>/<model name>_list.html

野生奥特曼 2024-12-12 19:41:29

您可以在 Dreamhost 上升级到 Django 1.3:blog.oscarcp.com/?p=167 –
jturnbull 9 月 22 日 9:54


这解决了我遇到的 urls.py 问题。一旦我升级到 1.3.1 并更改代码以反映它,我的页面就回来了。

You can upgrade to Django 1.3 on Dreamhost: blog.oscarcp.com/?p=167
jturnbull Sep 22 at 9:54

This fixed my issue with the urls.py issue I was having. Once I upgraded to 1.3.1 and changed the code to reflect it, my pages came back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文