gpt4 book ai didi

python - Django 教程第 3 部分出现此错误的原因是什么?

转载 作者:太空狗 更新时间:2023-10-29 22:31:03 25 4
gpt4 key购买 nike

我在学习 Django 教程时遇到了一个问题,特别是在向投票应用程序添加更多 View 时。作为引用,这是让我失望的部分的开头:https://docs.djangoproject.com/en/1.5/intro/tutorial03/#writing-more-views

在该部分之前,我可以毫无问题地显示投票/ View 。但是,当我添加三个额外的 View 以使 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'),
)

我得到一个错误。顺便说一句,我的网站 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 文档
  • 使用虚拟机
  • 使用虚拟环境

感谢任何帮助!为什么会出现此错误?

最佳答案

看起来 Python 无法导入 polls.urls - 这就是 __import__(name) 失败的原因。这里的“名称”将是您的模块名称“polls.urls”。

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

$ python manage.py shell

Python ... blah blah
...

> import polls.urls

这会失败,但是回溯会给你下一条线索,告诉你错误在哪里。

祝你好运!

关于python - Django 教程第 3 部分出现此错误的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17130026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com