Django urls uuid not working

Rajeev picture Rajeev · Oct 5, 2015 · Viewed 24k times · Source

In the following, if the url is set as ,what should be the pattern for uuid?

urls.py

url(r'^getbyempid/(?P<emp_id>[0-9]+)/(?P<factory_id>[0-9]+)$',views.empdetails)

Doesnt work,

http://10.0.3.79:8000/app1/getbyempid/1/b9caf199-26c2-4027-b39f-5d0693421506

but this works

http://10.0.3.79:8000/app1/getbyempid/1/2

Answer

vishes_shell picture vishes_shell · Dec 22, 2017

Since Django 2.0 you don't even need to worry about regex for UUID and int with new Django feature: Path Converters.

Make code elegant again:

from django.urls import path
...

urlpatterns = [
    ...
    path('getbyempid/<int:emp_id>/<uuid:factory_id>', views.empdetails)
]