[Django]-How do I match the question mark character in a Django URL?

40đź‘Ť

âś…

Don’t. You shouldn’t match query string with URL Dispatcher.
You can access all values using request.GET dictionary.

urls

(r'^pbanalytics/log/$', 'mydjangoapp.myFunction')

function

def myFunction(request) 
  param1 = request.GET.get('param1')
👤vartec

3đź‘Ť

Django’s URL patterns only match the path component of a URL. You’re trying to match on the querystring as well, this is why you’re having trouble. Your first regex does what you wanted, except that you should only ever be matching the path component.

In your view you can access the querystring via request.GET

👤bradley.ayers

2đź‘Ť

The ? character is a reserved symbol in regex, yes. Your first attempt looks like proper escaping of it.

However, ? in a URL is also the end of the path and the beginning of the query part (like this: protocol://host/path/?query#hash.
Django’s URL dispatcher doesn’t let you dispatch URLs based on the query part, AFAIK.

My suggestion would be writing a django view that does the dispatching based on the request.GET parameter to your view function.

👤peritus

1đź‘Ť

The way to do what the original question was i.e. catch-all in URL dispatch var…

url(r'^mens/(?P<pl_slug>.+)/$', 'main.views.mens',),

or

url(r'^mens/(?P<pl_slug>\?+)/$', 'main.views.mens',),

As far as why this is needed, GET URL’s don’t exactly provide good “permalinks” or good presentation in general for customers and to clients.

Clients often times request the url be formatted i.e.

www.example-clothing-site.com/mens/tops/shirts/t-shirts/Big_Brown_Shirt3XL

this is a far more readable interface for the end-user and provides a better overall presentation for the client.

👤user3608721

Leave a comment