Django url regex parameter entry

I want to redirect the following uri to a view;

localhost:8000/?tag=Python

to

def index_tag_query(request, tag=None):

in my conf url, I tried the following regex patterns, but no one seems to capture the request, although 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?

+4
source share
1 answer

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

Basically, urlconf parses and directs the URL into the view, passing all the GET parameters to the view. You are dealing 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": ... 
+6
source

Source: https://habr.com/ru/post/1393606/


All Articles