1

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?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • Does this answer your question? [Capturing URL parameters in request.GET](https://stackoverflow.com/questions/150505/capturing-url-parameters-in-request-get) – NKSM Sep 06 '21 at 08:17
  • @NKSM Not really sure about that as the question you showed alway works with "/?" and dont have any "/" beside I dont need to capture the parameters, I simply want it to match the regex –  Sep 06 '21 at 08:22
  • Have you tried that ? r'^stream/(?P[0-9a-f-=&?]+)$' – Mustofa Rizwan Sep 06 '21 at 08:24
  • Still leads to: django.urls.exceptions.NoReverseMatch: Reverse for 'stream' with keyword arguments '{'pk': '5409caac-fc9c-42b8-90af-058eff65a156?st=uARN1o52zWjBTPgGZmxyMw&e=1630917248'}' not found. 1 pattern(s) tried: ['stream/(?P[0-9a-f-=&?]+)$'] –  Sep 06 '21 at 08:30
  • Well, I got it working, like this: url(r'^stream/(?P[0-9a-zA-Z-=&?]+)$', Thze primary key was is uuid so its just [0-9a-f-]+ but everything behind it is upper and lower case a-z, A-Z,0-9 –  Sep 06 '21 at 08:32

1 Answers1

1

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 for myapp/.

In a request to https://www.example.com/myapp/?page=3, the URLconf will look for myapp/.

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.

  • No really sure onto that, for me it has to look like this: re_path(r'^stream/(?P[0-9a-zA-Z-=_&?]+)$ I generating the /stream url using a template decorator so django expects that this pattern matches upon rendering the template. But re_path is a good practice anyways. –  Sep 06 '21 at 09:02
  • What do you mean you are not sure with that? Have you tried `re_path`? I tried it with `re_path` and your original regex worked with no modification, no need to match the additional characters `"?"`, `"="`, and `"&"`. It is also as documented. – Niel Godfrey Pablo Ponciano Sep 06 '21 at 09:04