15

I'm at the last part of this tutorial.

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

The ListView works, but when I visit a url with DetailView, I get.

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

I'm not sure what I'm doing wrong. Any help would be appreciated.

EDIT: Add the main 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)),
)
yasith
  • 8,981
  • 7
  • 27
  • 32
  • It looks like there is no Poll object with pk (id) of 2. Go into /admin/ and look at all the Polls. – Rob Osborne Sep 06 '12 at 16:32
  • @RobOsborne I get the same error with http://127.0.0.1:8000/polls/1/ There is a poll with pk=1. – yasith Sep 06 '12 at 16:37
  • That is utterly bizarre, the block of code in Django 1.4.1 indicates that pk is not one of the arguments but I don't see how that could be. Is the urls.py code above your urls.py or just the code from the tutorial? There has to be something wrong with that urls.py file. Make sure you restarted your test server. – Rob Osborne Sep 06 '12 at 20:25
  • It's the urls.py straight from the tutorial. I tried restarting the server as well. Same result. – yasith Sep 06 '12 at 20:30
  • Can you post the urls.py from the top level as well? What database are you using for the tutorial? – Rob Osborne Sep 06 '12 at 22:01
  • I edited the question to include the top-level urls.py file. I'm using a sqlite3 database. It's been working fine until I started using Generic Views, something to note is that the ListView is working properly. – yasith Sep 07 '12 at 01:47

2 Answers2

40

I think the code you posted above, is not the one you have on your disk.

I had the same problem, but then I looked carefully at both, my code and the tutorial. The regex I had in my code was different from the tutorial.

This was my code:

 url(r'^(?P<poll_id>\d+)/$',-$                                               
 url(r'^(?P<poll_id>\d+)/results/$',-$                                       

This is the correct core:

 url(r'^(?P<pk>\d+)/$',-$                                               
 url(r'^(?P<pk>\d+)/results/$',-$                                       

Note that *poll_id* was in the previous sections of the tutorial, but generic views require pk. Also note that the tutorial is correct, and you posted the correct code (from the tutorial.)

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
nbensa
  • 416
  • 5
  • 3
  • 1
    Thank you! I focused on the view functions and made the correct changes there, but failed to notice that they had changed the regexes as well. I guess I just went too fast. Perhaps they should emphasize (and explain) this other change in the tutorial text... oh well. – aldo Sep 06 '13 at 06:09
  • Same here. This sort of thing gets right on my nerves with tutorials, and it's happened a few times with Django-related ones. If you're telling me to replace some code, show a diff of what has changed, especially if it's 2 characters hidden in the middle of a line... – Rikki Jul 21 '14 at 22:28
0

Look Carefully in the Tutorials they have mentioned to change the urlpatterns to use primarykey instead of question_id.

Naman
  • 183
  • 12