Reputation:
I have the following endpoint at my urls.py
url(r'^stream/(?P<pk>[0-9a-f-]+)$', App_Views.stream, name='stream'),
That would mean that the endpoint would look like this on call:
http://localhost/stream/5409caac-fc9c-42b8-90af-058eff65a156
Now I'm adding some extra parameters to the URL before calling it, so that it looks like this:
http://localhost/stream/5409caac-fc9c-42b8-90af-058eff65a156?st=zD3H0cHJrbcUAHZPNbPGyg&e=1630915404
Now to my actual question, how does the regex at urls.py has to look like in order to accept the second URL example?
Upvotes: 1
Views: 1717
Reputation: 10699
The usage of url()
is deprecated already. Use re_path()
instead.
Given this is your URL:
http://localhost/stream/5409caac-fc9c-42b8-90af-058eff65a156?st=zD3H0cHJrbcUAHZPNbPGyg&e=1630915404
I assume the extra parameters are specifically:
?st=zD3H0cHJrbcUAHZPNbPGyg&e=1630915404
All of that string that starts from the character ?
is already part of the query string parameters. In Django, the URL pattern matching is only for the actual path, not including the query string. This is documented here:
What the URLconf searches against
The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name.
For example, in a request to
https://www.example.com/myapp/
, the URLconf will look formyapp/
.In a request to
https://www.example.com/myapp/?page=3
, the URLconf will look formyapp/
.The URLconf doesn’t look at the request method. In other words, all request methods – POST, GET, HEAD, etc. – will be routed to the same function for the same URL.
So this should still work:
re_path(r'^stream/(?P<pk>[0-9a-f-]+)$', App_Views.stream, name='stream'),
If you need to access the values of the query string, access the request.GET
within the receiving view.
Upvotes: 1