首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Django教程:通用视图。属性错误

Django教程:通用视图。属性错误
EN

Stack Overflow用户
提问于 2012-09-07 00:03:22
回答 1查看 9.2K关注 0票数 15

我已经完成了this教程的最后一部分。

from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll

urlpatterns = patterns('',
    url(r'^$',
        ListView.as_view(
            queryset=Poll.objects.order_by('-pub_date')[:5],
            context_object_name='latest_poll_list',
            template_name='polls/index.html')),
    url(r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/detail.html')),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/results.html'),
        name='poll_results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)

ListView可以工作,但当我用DetailView访问url时,我会得到。

AttributeError at /polls/2/
Generic detail view DetailView must be called with either an object pk or a slug.
Request Method: GET
Request URL:    http://127.0.0.1:8000/polls/2/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:    
Generic detail view DetailView must be called with either an object pk or a slug.
Exception Location: /home/yasith/coding/django/django-tutorial/lib/python2.7/site-packages/django/views/generic/detail.py in get_object, line 46
Python Executable:  /home/yasith/coding/django/django-tutorial/bin/python2
Python Version: 2.7.3

我不确定我做错了什么。任何帮助都将不胜感激。

编辑:添加主urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),
)
EN

回答 1

Stack Overflow用户

发布于 2014-05-27 02:01:56

仔细查看他们提到的教程,将use模式更改为使用primarykey而不是question_id。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12304050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档