开发者

django url regex doesn't match

Not sure wh开发者_JAVA百科y this doesn't match any urls in urls.py. I checked with a regex checker and it should be correct.

urls.py:

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

sample url:

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

Checked using this simple regex checked. Seems right. Any ideas?

Thanks!


the urlconf isn't used to match the request.GET parameters of your url. You do that within the view.

you either want your urls to look like this:

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

and match it using:

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

with your view that looks like:

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

or

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

and your urls.py:

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

and 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜