gpt4 book ai didi

python - 无法配置 View ,urls.py SyntaxError : invalid syntax

转载 作者:行者123 更新时间:2023-12-01 05:28:27 24 4
gpt4 key购买 nike

我在配置 View 时遇到问题,我正在关注 django 1.5 官方教程。这是我的 polls/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'),

)

下面是我的 polls/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)

在 polls/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'), ^语法错误:语法无效[2013年12月31日06:06:34]“获取/admin/HTTP/1.1”500 84890

请帮忙。

最佳答案

在你的代码中

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')之后错过了一个逗号。

关于python - 无法配置 View ,urls.py SyntaxError : invalid syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858682/

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