[Django]-Django urls uuid not working

52πŸ‘

βœ…

As well as the digits 0-9, the uuid can also include digits a-f and hyphens, so you could should change the pattern to

(?P<factory_id>[0-9a-f-]+)

You could have a more strict regex, but it’s not usually worth it. In your view you can do something like:

try:
    factory = get_object_or_404(Factory, id=factory_id)
except ValueError:
    raise Http404

which will handle invalid uuids or uuids that do not exist in the database.

πŸ‘€Alasdair

78πŸ‘

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)
]
πŸ‘€vishes_shell

6πŸ‘

Just to complete other answers, please note that the Regex should be a-f not a-z, so:

urlpatterns = [
    url(r'^request/(?P<form_id>[0-9A-Fa-f-]+)', views.request_proxy)
]

something like above could be the most accurate answer.

1πŸ‘

Your url patterns is taking only numbers, try this one:

url(r'^getbyempid/(?P<emp_id>[0-9a-z-]+)/(?P<factory_id>[0-9a-z-]+)$',views.empdetails)
πŸ‘€Geo Jacob

0πŸ‘

had the same problem,
fixed it with this :

url(r'^offer_details/(?P<uuid>[0-9a-f\-]{32,})$', offer_details, name='offer_details')
`
πŸ‘€lansanalsm

Leave a comment