Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django url regex parameter capturing

Tags:

python

django

I want to route the following uri to a view;

localhost:8000/?tag=Python

to

def index_tag_query(request, tag=None):

in my url conf, I've tried the following regex patterns but none seem to be capturing the request even though the regex looks good;

url(r'^\?tag=(?P<tag>\w+)/$', 'links.views.index_tag_query'),

url(r'^\/?\?tag=(?P<tag>\w+)/$', 'links.views.index_tag_query'),

url(r'^\/?\?tag=(?P<tag>.*)/$', 'links.views.index_tag_query'),

What gives?

like image 641
Kevin Avatar asked Jan 29 '12 11:01

Kevin


1 Answers

You can't parse GET parameters from your URLconf. For a better explanation then I can give, check out this question (2nd answer): Capturing url parameters in request.GET

Basically, the urlconf parses and routes the URL to a view, passing any GET parameters to the view. You deal with these GET parameters in the view itself

urls.py

url(r^somepath/$', 'links.views.index_tag_query')

views.py

def index_tag_query(request):
    tag = request.GET.get('tag', None)
    if tag == "Python":
         ...
like image 60
Timmy O'Mahony Avatar answered Sep 28 '22 13:09

Timmy O'Mahony