Django urls uuid not working

Further, if the url is given as, what should be the template for uuid?

urls.py

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

Does not work,

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

but it works

http://10.0.3.79:8000/app1/getbyempid/1/2
+12
source share
5 answers

Just like the numbers 0-9, uuid can also contain the numbers af and hyphens, so you can change the pattern to

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

You may have a more strict regular expression, but it is usually not worth it. In your opinion, 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 are not in the database.

+36
source

Starting with Django 2.0, you don’t even have to worry about regular expressions for UUIDs and ints with the new Django function: Path Converters .

:

from django.urls import path
...

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

, , Regex a-f not a-z, :

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

- .

+5

Your url patterns only accept numbers, try the following:

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

had the same problem, fixed it as follows:

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

Source: https://habr.com/ru/post/1610275/


All Articles