Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for uuid hex value in django urls

I am generating a uuid value and want to receive it from url in django, but the regex i am writing is not working and telling me page not found

import uuid
id = uuid.uuid4().hex
print id
be00e546822945bbb193b4ed80149c35

urls.py

urlpatterns = [
       url(r'^users/(?P<user_id>[0-9a-f]{32}\Z)/$', UserDetails.as_view(), name = 'users_detail'),
]

Result:

"GET /users/be00e546822945bbb193b4ed80149c35/ HTTP/1.1" 404 3302

So what would be the correct regex for uuid hex value in urls.py ?

like image 719
Shiva Krishna Bavandla Avatar asked Oct 28 '25 17:10

Shiva Krishna Bavandla


1 Answers

If you want to really check that you have UUID, try this one:

[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}

I've found it a while ago on SO and used it for a while to check that data is actually regexp. Haven't used it Django URLS directly, but I think it should be fine.

like image 91
Andrey Rusanov Avatar answered Oct 31 '25 06:10

Andrey Rusanov