Django教程:应用通用视图后出错'无模块名为polls'

时间:2013-03-21 21:40:19

标签: django module django-generic-views

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

我正在关注“https://docs.djangoproject.com/en/1.5/intro/tutorial04/

中提供的教程

在将通用视图代码应用于'polls / urls.py'之前,一切正常。现在我在webbrowser窗口中出现以下错误:

@ 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

我已经阅读了问题并回答了@'Django official tutorial for the absolute beginner, absolutely failed!',现在更加困惑了。这是指教程文档的一部分,并建议:

“浏览模板,修改对latest_poll_list到object_list的任何引用,并将对poll的任何引用更改为object。”

但是,我使用的教程文档的版本似乎表明可以通过设置来方便地避免这种情况:

context_object_name='latest_poll_list'  

我的民意调查/ urls.py目前看起来像这样:

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

我的民意调查/ views.py看起来像这样:

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

这是我错过的拼写错误还是我确实需要更改HTML文件中的引用?非常感谢所有的建议。

1 个答案:

答案 0 :(得分:0)

您是否将命名空间添加到根urls.py?

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

修改 尝试将patterns调用中的space参数更改为空字符串:

urlpatterns=patterns(' ',

urlpatterns=patterns('',