2014-10-09 75 views
2

我想将表的ID传递给我的函数,但我不知道发生了什么。 如果我硬编码身份证号码工作,如果我使用(?Pd +)与D +,因此它使用尽可能多的数字,如在教程中。不起作用。这应该是不同的?django 1.7如何将参数传递到函数正则表达式

谢谢你们。

我的网址

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 

回答

1

您的表达式中缺少反斜杠,目前d+与字符d字面上的“一个或多个”时间匹配。反斜杠结合文字字符创建具有特殊含义的正则表达式标记。

因此,\d+将匹配数字09“一个或多个”时间。

url(r'^vote/Restaurant_Info/(?P<rest_id>\d+)/$', views.restaurant_menu) 
+0

感谢您的解释,我因子评分是Django的,但我要寻找到RE,它的工作原理为字符串以同样的方式?像url(r'^/vote/thanks /(?P \ w +)/ $',views.thanks), – pelos 2014-10-09 22:39:00

+0

是的。这里有一个很好的参考文献[http://www.regular-expressions.info/](http://www.regular-expressions.info/) – hwnd 2014-10-09 22:42:00

0

你缺少一个斜杠。它应该是(?P<rest_id>\d+)

0
url(r"^vote/Restaurant_Info/(?P<rest_id>\d+)/$", views.restaurant_menu), 
相关问题