I generate my URLs dynamically and I want to know if there is a way to check if the syntax is valid or not. Because I’ll create my URL from the Django admin panel, and if the syntax isn’t correct for Django, the code will crash. For example, if I make a mistake and create a URL like this:
test/<:custom_pk>/`, the app will crash, and I won’t be able to modify it from the admin panel because of the crash.
The error is :
File “usr/local/lib/python3.12/site-packages/django/urls/resolvers.py”, line 307, in init
self.converters = _route_to_regex(str(route), is_endpoint)[1]
File “usr/local/lib/python3.12/site-packages/django/urls/resolvers.py”, line 277, in _route_to_regex
raise ImproperlyConfigured(
django.core.Exceptions.ImproperlyConfigured: URL route ‘test/<:custom_id>’ uses parameter ‘<:custom_id>’ which isn’t a valid Python identifier.
I try to send to my urlpatterns an url which had an incorrect syntax and i except this error :
File “usr/local/lib/python3.12/site-packages/django/urls/resolvers.py”, line 307, in init
self.converters = _route_to_regex(str(route), is_endpoint)[1]
File “usr/local/lib/python3.12/site-packages/django/urls/resolvers.py”, line 277, in _route_to_regex
raise ImproperlyConfigured(
django.core.Exceptions.ImproperlyConfigured: URL route ‘test/<:custom_id>’ uses parameter ‘<:custom_id>’ which isn’t a valid Python identifier.
Othmane AIT SALAH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.