【发布时间】:2014-10-09 22:24:28
【问题描述】:
我正在尝试将表的 ID 传递给我的函数,但我不确定发生了什么。 如果我对 ID 号进行硬编码,如果我将 (?Pd+) 与 d+ 一起使用,则它使用尽可能多的数字,就像在教程中一样。不起作用。这应该不同吗?
谢谢大家。
我的网址
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
【问题讨论】: