[Django]-Changing a old Django URL to the new paths

4👍

There is no straight conversion for your path you could either use a converter as stated in the docs to convert the token. Here the example from the docs:

class FourDigitYearConverter:
    regex = '[0-9]{4}'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return '%04d' % value

register the converter

from django.urls import path, register_converter

from . import converters, views

register_converter(converters.FourDigitYearConverter, 'yyyy')

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    path('articles/<yyyy:year>/', views.year_archive),
    ...
]

or you can just regex the path like you currently are:

from django.urls import path, re_path

from . import views

urlpatterns = [
    path('articles/2003/', views.special_case_2003),
    re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', views.activate, name='activate')
]

I would just stick to the regex using re_path since you know it works and its already done.

Here is the link to the docs:
https://docs.djangoproject.com/en/2.0/topics/http/urls/

👤Taylor

Leave a comment