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?

5
  • Does this answer your question? Capturing URL parameters in request.GET Commented Sep 6, 2021 at 8: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 Commented Sep 6, 2021 at 8:22
  • Have you tried that ? r'^stream/(?P<pk>[0-9a-f-=&?]+)$' Commented Sep 6, 2021 at 8: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<pk>[0-9a-f-=&?]+)$'] Commented Sep 6, 2021 at 8:30
  • Well, I got it working, like this: url(r'^stream/(?P<pk>[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 Commented Sep 6, 2021 at 8:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No really sure onto that, for me it has to look like this: re_path(r'^stream/(?P<pk>[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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.