姜戈教程。 404 关于通用视图
更新:使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Django 1.3 引入了基于类的通用视图,它将取代这种基于函数的方法(请参阅 文档页面)所以也许最好使用它们。
使用基于类的方法,您的新详细信息页面 url 将如下所示:
此方法可以在 教程的第 4 部分。
注意:我倾向于不将
template_name
参数传递给as_view
因为,如文档中所述: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:
This approach can be found in part 4 of the tutorial.
N.B.: I tend not to pass the
template_name
argument toas_view
because, as stated in the docs:这解决了我遇到的 urls.py 问题。一旦我升级到 1.3.1 并更改代码以反映它,我的页面就回来了。
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.