Django URL 配置
现在我还在Django教程的第三部分:
http://docs.djangoproject.com/en/1.1/intro/tutorial03/#intro-tutorial03
我正在尝试用教程提供的这段代码来设置urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/$', 'mysite.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
(r'^admin/', include(admin.site.urls)),
)
如果我把默认的urls.py(里面什么都没有)换成这段代码,访问127.0.0.1:8000/polls/是可以的,但奇怪的是,访问127.0.0.1:8000/admin就不行了,出现了以下错误:
异常类型: TemplateSyntaxError
异常信息: 渲染时捕获到异常:在模块mysite.polls.views中尝试调用vote。错误是:'module'对象没有属性'vote'
And this (Error line 30):
Caught an exception while rendering: Tried vote in module mysite.polls.views. Error was: 'module' object has no attribute 'vote'
20 <!-- Header -->
21 <div id="header">
22 <div id="branding">
23 {% block branding %}{% endblock %}
24 </div>
25 {% if user.is_authenticated and user.is_staff %}
26 <div id="user-tools">
27 {% trans 'Welcome,' %}
28 <strong>{% firstof user.first_name user.username %}</strong>.
29 {% block userlinks %}
**30 {% url django-admindocs-docroot as docsroot %}**
31 {% if docsroot %}
32 <a href="{{ docsroot }}">{% trans 'Documentation' %}</a> /
33 {% endif %}
34 {% url admin:password_change as password_change_url %}
35 {% if password_change_url %}
36 <a href="{{ password_change_url }}">
37 {% else %}
38 <a href="{{ root_path }}password_change/">
39 {% endif %}
40 {% trans 'Change password' %}</a> /
我觉得错误可能出在这里:
(r'^admin/', include(admin.site.urls)),
但我找不到问题所在。
谢谢关注!!
1 个回答
2
就是Django好像找不到你模块里叫做 vote
的那个函数,模块名是 views
。