rabbid
rabbid

Reputation: 5725

django url regex doesn't match

Not sure why 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!

Upvotes: 0

Views: 990

Answers (1)

dting
dting

Reputation: 39287

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)
    ...

Upvotes: 4

Related Questions