Update URL dispatching to new path method in django >= 2.0 #2596

Closed
opened 2025-12-29 18:20:15 +01:00 by adam · 0 comments
Owner

Originally created by @Grokzen on GitHub (May 8, 2019).

Proposed Changes

In django >= 2.0 there is a simpler form of url routing syntax that was introduced in 2.0 and expanded in 2.1. The basic example and changelog item can be found here https://docs.djangoproject.com/en/2.0/releases/2.0/#simplified-url-routing-syntax

The basic example of the change is

The new django.urls.path() function allows a simpler, more readable URL routing syntax. For example, this example from previous Django releases:

url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),

could be written as:

path('articles/<int:year>/', views.year_archive),

It simplifies the syntax some and removes a few things that is not needed in this simpler form.

But the main advantage is the simpler format of getting out simple variables from the URL. In the old way you needed to provide a regex for all types. But in this new format you get 5 built-in common types str, int, slug, uuid, path see https://docs.djangoproject.com/en/2.1/topics/http/urls/#path-converters And you can also build your own path converters if you have some custom format or need a new type like FourDigitYearConverter as shown in the example.

For all other cases you have access to a new method re_path that provides both things if you can't convert regexes right away.

Justification

url() is in 2.2 a shortcut to re_path and django states that this method will be deprecated in a future release. https://docs.djangoproject.com/en/2.2/ref/urls/#url

Originally created by @Grokzen on GitHub (May 8, 2019). <!-- NOTE: This type of issue should be opened only by those reasonably familiar with NetBox's code base and interested in contributing to its development. Describe the proposed change(s) in detail. --> ### Proposed Changes In django >= 2.0 there is a simpler form of url routing syntax that was introduced in 2.0 and expanded in 2.1. The basic example and changelog item can be found here https://docs.djangoproject.com/en/2.0/releases/2.0/#simplified-url-routing-syntax The basic example of the change is ``` The new django.urls.path() function allows a simpler, more readable URL routing syntax. For example, this example from previous Django releases: url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), could be written as: path('articles/<int:year>/', views.year_archive), ``` It simplifies the syntax some and removes a few things that is not needed in this simpler form. But the main advantage is the simpler format of getting out simple variables from the URL. In the old way you needed to provide a regex for all types. But in this new format you get 5 built-in common types `str, int, slug, uuid, path` see https://docs.djangoproject.com/en/2.1/topics/http/urls/#path-converters And you can also build your own path converters if you have some custom format or need a new type like `FourDigitYearConverter` as shown in the example. For all other cases you have access to a new method `re_path` that provides both things if you can't convert regexes right away. <!-- Provide justification for the proposed change(s). --> ### Justification `url()` is in 2.2 a shortcut to `re_path` and django states that this method will be deprecated in a future release. https://docs.djangoproject.com/en/2.2/ref/urls/#url
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:20:15 +01:00
adam closed this issue 2025-12-29 18:20:15 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2596