我正试图将表的ID传递给我的函数,但我不确定发生了什么。如果我对ID号进行硬编码,如果我将(?Pd+)与d+一起使用,那么它会使用尽可能多的数字,就像教程中一样。不起作用。这应该有所不同吗?
谢谢大家。
我的URL
from django.conf.urls import patterns, include, url
from polls import views
urlpatterns = patterns('',
#url(r'^main_site/$', views.main_site),
url(r'^vote/$', views.vote),
url(r'^stadistics/$', views.stadistics),
# using it like this doesn't work
url(r'^vote/Restaurant_Info/(?P<rest_id>d+)/$', views.restaurant_menu),
#testing the info of the restaurant
# hard coding the id of the restaurant does work
url(r'^vote/Restaurant_Info/4/$', views.restaurant_menu),
我的观点
def restaurant_menu(request, rest_id="0"):
response = HttpResponse()
try:
p = Restaurant.objects.get(id=rest_id)
response.write("<html><body>")
response.write("<p>name of the restaurant</p>")
response.write(p.name)
response.write("</body></html>")
except Restaurant.DoesNotExist:
response.write("restaurant not found")
return response
表达式中缺少一个反斜杠,当前d+
与字符d
匹配"一次或多次"。反斜杠与文字字符组合可创建具有特殊含义的正则表达式标记。
因此,d+
将匹配数字0
到9
"一次或多次"。
url(r'^vote/Restaurant_Info/(?P<rest_id>d+)/$', views.restaurant_menu)
您缺少一个斜杠。它应该是(?P<rest_id>d+)
url(r"^vote/Restaurant_Info/(?P<rest_id>d+)/$", views.restaurant_menu),