2011-05-03 111 views
0

不确定为什么这不匹配urls.py中的任何url。我检查了一个正则表达式检查器,它应该是正确的。django url正则表达式不匹配

urls.py:

url(r'^toggle_fave/\?post_id=(?P<post_id>\d+)$', 'core.views.toggle_fave', name="toggle_fave"), 

样品网址:

http://localhost:8000/toggle_fave/?post_id=7

使用this简单的regex检查托运。看起来没错。有任何想法吗?

谢谢!

回答

4

urlconf不用于匹配你的url的request.GET参数。你在视图中这样做。

你要么希望自己的网址是这样的:

http://localhost:8000/toggle_fave/7/ 

,并使用与之相匹配:

url(r'^toggle_fave/(?P<post_id>\d+)/$', 'core.views.toggle_fave', name="toggle_fave"), 

你的观点,看起来像:

def toggle_fave(request, post_id): 
    post = get_object_or_404(Post, pk=post_id) 
    ... 

http://localhost:8000/toggle_fave/?post_id=7 

和你的urls.py:

url(r'^toggle_fave/$', 'core.views.toggle_fave', name="toggle_fave"), 

和views.py:

def toggle_fave(request): 
    post_id = request.GET.get('post_id', '') 
    if post_id: 
     post = get_object_or_404(Post, pk=post_id) 
    ... 
+0

好吧,我明白了,谢谢你。尽管我认为urls.py中的正则表达式部分是误导性的。 – rabbid 2011-05-03 03:30:32