Coo*_*bes 0 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')
)
Run Code Online (Sandbox Code Playgroud)
特别是它似乎在告诉我:
from venues import views
Run Code Online (Sandbox Code Playgroud)
行不正确.
但是我的场地/ 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)
Run Code Online (Sandbox Code Playgroud)
所以文件存在并且似乎做得很好,直到我开始在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')),
)
Run Code Online (Sandbox Code Playgroud)
所以我不完全确定问题的来源,所有帮助都表示赞赏.
非常感谢,我.
您的view.py文件确实存在语法错误:
return HttpResponse("You're looking at Venue %s.", % venue_id)
Run Code Online (Sandbox Code Playgroud)
和
return HttpResponse("You're looking at events at venue %s.", % venue_id)
Run Code Online (Sandbox Code Playgroud)
字符串和%字符串格式化运算符之间有逗号,这不是合法的python语法.在%运算符之前删除逗号.
将来,在报告Python代码问题时,请始终包含完整的 python回溯,这样我们就不必猜测您的问题是什么.