Django说我的语法错了

时间:2012-12-05 12:37:23

标签: python django syntax module

基本上使用django,它一直告诉我这段代码中存在语法错误:

from django.conf.urls import patterns, url

from venues import views

urlpatterns = patterns('',
    url(r'^$', views.index, name = 'index'),

    # ex /venues/3
    url(r'^(?P<venue_id>\d+)/$', views.detail, name='detail'),
    # ex: /venues/3/events
    url(r'^(?P<venue_id>\d+)/events/$', views.events, name='events')
)

特别是它似乎在告诉我:

from venues import views

行不正确。

但是我的场地/ views.py看起来像是:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello this is the home page!")

def detail(request, venue_id):
    return HttpResponse("You're looking at Venue %s.", % venue_id)

def events(request, venue_id):
    return HttpResponse("You're looking at events at venue %s.", % venue_id)

所以该文件存在并且似乎做得很好,直到我开始在urls.py中使用venue_id

哦,只是为了衡量我的主要urls.py看起来像:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'Comedy.views.home', name='home'),
    # url(r'^Comedy/', include('Comedy.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(r'^venues/', include('venues.urls')),
)

所以我不能完全确定问题的来源,所有帮助都有所帮助。

非常感谢, 我

3 个答案:

答案 0 :(得分:6)

您的view.py文件确实存在语法错误:

return HttpResponse("You're looking at Venue %s.", % venue_id)

return HttpResponse("You're looking at events at venue %s.", % venue_id)

在字符串和%字符串格式化运算符之间使用逗号,这不是合法的python语法。删除%运算符之前的逗号。

将来,在报告Python代码问题时,请始终包含完整的 python traceback,这样我们就不必猜测您的问题是什么。

答案 1 :(得分:1)

我认为错误就在这里:

def detail(request, venue_id):
    return HttpResponse("You're looking at Venue %s.", % venue_id)

def events(request, venue_id):
    return HttpResponse("You're looking at events at venue %s.", % venue_id)

你必须在%venue_id之前删除逗号。

答案 2 :(得分:1)

而不是:

def events(request, venue_id):
    return HttpResponse("You're looking at events at venue %s.", % venue_id)

尝试:

def events(request, venue_id):
    return HttpResponse("You're looking at events at venue %s.")  % venue_id