2012-05-07 138 views
0

我怀疑它可能与我正在对我的正则表达式进行样式设置有关,因为我尝试访问时得到以下输出:http://127.0.0.1:8000/recipes/search/fish/例如...在Django中通过URL传递参数时找不到页面

使用gfp.urls定义的URL配置,Django的尝试这些URL模式,顺序如下:

^recipes/$ 
^recipes/category/(?P<category>\d+)/$ 
^recipes/search/(?P<term>\d+)/$ 
^recipes/view/(?P<slug>\d+)/$ 
^admin/ 

当前的URL,食谱/搜索/鱼/,不符合任何这些。

仅供参考,这里是我的URLconf

urlpatterns = patterns('', 
url(r'^recipes/', 'main.views.recipes_all'), 
url(r'^recipes/category/(?P<category>\d+)/$', 'main.views.recipes_category'), 
url(r'^recipes/search/(?P<term>\d+)/$', 'main.views.recipes_search'), 
url(r'^recipes/view/(?P<slug>\d+)/$', 'main.views.recipes_view'), 

仅供参考下面是我attemping在

def recipes_all(request): 
    return HttpResponse("this is all the recipes") 

def recipes_category(request, category): 
    return HttpResponse("this is the recipes category % s" % (category)) 

def recipes_search(request, term): 
    return HttpResponse("you are searching % s in the recipes" % (term)) 

def recipes_view(request, slug): 
    return HttpResponse("you are viewing the recipe % s" % (slug)) 

我怀疑这是我的正则表达式的那一刻使用的意见,会有人能够请解释它有什么问题吗?我已经看到/ w的一些URL正则表达式中使用,但它不进入它的Django的tuorial这里(?):

https://docs.djangoproject.com/en/1.4/intro/tutorial03/

回答

2

'^recipes/search/(?P<term>\d+)/$'比赛/recipes/search/123456/'^recipes/search/(?P<term>[-\w]+)/$'也可能是你所需要的。 (用连字符更新)

看看Python re docs了解'\ d','\ w'和其他的含义。

+1

谢谢!它的工作原理是\ d数字和\ w char?我怎么能允许连字符 - 这个,因为它看起来像\ w不会接受这个?我想\ d不是吗? – jdx

+0

'\ w'通常是任何字母数字字符和下划线 – San4ez

+0

为了允许连字符使用'[ - \ w]' –

1

对于recipe/search,您的urlpattern只允许搜索字词的数字(\d)。更改为\w,你应该很好。

+0

谢谢! \ d数字和\ w char或varchar? – jdx