【问题标题】:Unable to configure views, urls.py SyntaxError: invalid syntax无法配置视图,urls.py SyntaxError: invalid syntax
【发布时间】:2014-01-18 11:14:46
【问题描述】:

我在配置视图时遇到问题,我正在关注 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'),

)

下面是我的投票/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'), ^ SyntaxError:无效的语法 [2013 年 12 月 31 日 06:06:34] "GET /admin/ HTTP/1.1" 500 84890

请帮忙。

【问题讨论】:

    标签: python django


    【解决方案1】:

    在您的代码中

    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')之后的逗号。

    【讨论】:

    • 它给了我同样的错误......我已经纠正了它。似乎问题出在views.vote URL配置中。
    • 我真的很抱歉给您带来不便,实际上我已经一次又一次地输入代码,所以这就是为什么会犯一些小错误。
    • 但是它再次给了我错误。文件“C:\Python27\Scripts\mysite\polls\views.py”,第 7 行 SyntaxError:第 7 行文件 C:\Python27\Scripts\mysite\polls\views.py 中的非 ASCII 字符 '\xe2',但没有声明编码;详见python.org/peps/pe p-0263.html [31/​​Dec/2013 07:08:37] "GET /admin/ HTTP/1.1" 500 97589
    • # -*- coding: utf-8 -*-添加到第一行视图。
    • 试过了,它在它添加的特定行上给了我错误。从过去 3 小时到现在,也尝试了很多解决方案。
    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2022-12-12
    相关资源
    最近更新 更多