在Django教程的第3部分导致此错误的原因是什么?

我在处理Django教程时遇到了一个问题,特别是在向poll应用程序添加更多视图时.作为参考,这是让我兴奋的部分的开头:https://docs.djangoproject.com/en/1.5/intro/tutorial03/#writing-more-views

在该部分之前,我可以让民意调查/视图显示没有问题.但是当我添加三个额外的视图来使polls / views.py看起来像这样:

def detail(request, poll_id):
    return HttpResponse("You're looking at poll %s." % poll_id)

def results(request, poll_id):
    return HttpResponse("You're looking at the results of poll %s." % poll_id)

def vote(request, poll_id):
    return HttpResponse("You're voting on poll %s." % poll_id)

然后使polls / urls.py看起来像这样:

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    # ex: /polls/
    url(r'^$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
    # ex: /polls/5/results/
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)

我收到一个错误.顺便说一下,我的mysite ROOT_URLCONF指向mysite / 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)),
)

我得到的错误是:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/polls/34

Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'polls')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  92.                     response = middleware_method(request)
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/middleware/common.py" in process_request
  69.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in is_valid_path
  551.         resolve(path, urlconf)
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  440.     return get_resolver(urlconf).resolve(path)
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/matthew/djangopoll/mysite/mysite/urls.py" in <module>
  8.                        url(r'^polls/', include('polls.urls')),
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
  25.         urlconf_module = import_module(urlconf_module)
File "/home/matthew/.virtualenvs/djangopoll/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)

Exception Type: SyntaxError at /polls/34
Exception Value: invalid syntax (urls.py, line 9)

此外,以下内容可能提供信息:

>使用Django 1.5
>使用Python 2.7.3
>使用Django 1.5文档
>使用VirtualBox
>使用VirtualEnv

任何帮助表示赞赏!为什么我收到此错误?

最佳答案
看起来Python无法导入polls.urls – 这就是__import __(name)失败的原因.这里的“名称”将是您的模块名称’polls.urls’.

要找出系统无法导入polls.urls的原因,请尝试以交互方式导入它.

$python manage.py shell

 Python ... blah blah
 ...

> import polls.urls

这将失败,但回溯将为您提供错误所在位置的下一个线索.

祝好运!

转载注明原文:在Django教程的第3部分导致此错误的原因是什么? - 代码日志