0

基本上使用 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 中使用场地 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')),
)

所以我不完全确定问题出在哪里,感谢所有帮助。

非常感谢,我。

4

3 回答 3

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 回溯,这样我们就不必猜测您的问题是什么。

于 2012-12-05T12:41:09.963 回答
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 之前的逗号。

于 2012-12-05T12:42:32.710 回答
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
于 2012-12-05T14:59:35.487 回答