无法配置视图,urls.py语法错误:语法无效

时间:2013-12-31 14:14:17

标签: python django

我在配置视图时遇到问题,我正在关注django 1.5官方教程。 这是我的民意调查/ urls.py。

from django.conf.urls import patterns, url

from polls import views

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

)

以下是我的民意调查/ views.py

 from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. You're at the poll index.")

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)

在民意调查/ urls.py我也试过了     url(r'^(?P \ d +)/ detail / $',views.detail,name ='detail'), 代替     url(r'^(?P \ d +)/ $',views.detail,name ='detail'), 我得到的错误是

文件“C:\ Python27 \ Scripts \ mysite \ polls \ urls.py”,第7行     url(r'^(?P \ d +)/ $',views.detail,name ='detail'),       ^ SyntaxError:语法无效 [2013年12月31日06:06:34]“GET / admin / HTTP / 1.1”500 84890

请帮忙。

1 个答案:

答案 0 :(得分:2)

在您的代码中

url(r'^(?P<poll_id>\d+/vote/$', views.vote, name='vote'),

应该是

url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),

您错过了父母身份,而您在url(r'^$', views.index, name='index')之后错过了一个逗号。