Question

Windows 7, 64bit
Python 2.7.3
Django 1.5
python manage.py runserver

I am following the tutorial as available at 'https://docs.djangoproject.com/en/1.5/intro/tutorial04/'

Everything was working fine until I applied the generic view code to 'polls/urls.py'. Now I get the following errors in my webbrowser windows:

@ index.py "No module named polls" - Screen shot from Chrome @ 127.0.0.1:8000/polls/

@ detail.py "No module named polls" - Screen shot from IE @ 127.0.0.1:8000/polls/1

I have read the question and answer @ 'Django official tutorial for the absolute beginner, absolutely failed!' and am now even more confused. This refers to a section of the tutorial documentation and suggests:

"Go through your templates, and modify any reference to latest_poll_list to object_list, and change any reference to poll to object."

However, the version of the tutorial documentation I am using seems to suggest that this can be conveniently avoided by setting:

context_object_name='latest_poll_list'  

My polls/urls.py currently looks like this:

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

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

My polls/views.py looks like this:

from django.http import Http404, HttpResponseRedirect  
from django.template import Context, loader  
from django.shortcuts import render, render_to_response, get_object_or_404  
from django.core.urlresolvers import reverse  
from polls.models import Poll, Choice  

def vote(request,poll_id):  
  p=get_object_or_404(Poll, pk=poll_id)  
  try:  
    selected_choice=p.choice_set.get(pk=request.POST['choice'])  
  except (KeyError, Choice.DoesNotExist):  
    return render(request, 'polls/detail.html',{  
      'poll':p,  
      'error_message': "You didn't select a choice.",  
    })  
  else:  
    selected_choice.votes+=1  
    selected_choice.save()  
    return HttpResponseRedirect(reverse('polls:results',args=(p.id,)))  

Is it a typo I have missed or do I indeed need to change references in the HTML files? Many thanks for all suggestions.

Was it helpful?

Solution

Did you add the namespace to your root urls.py?

 url(r'^polls/', include('polls.urls', namespace="polls")),

Edit: Try changing the space argument in the patterns call to an empty string:

urlpatterns=patterns(' ',

to

urlpatterns=patterns('',
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top