3

So I am making a new site in Django 2.0 and was following this tutorial on making a user registration form with an activation email and my understanding of the new Django 2 is not good enough so was asking what would be the Django 2 equivalent of this URL

url(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'),
Kareem
  • 569
  • 2
  • 18

1 Answers1

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
  • 1,223
  • 1
  • 15
  • 30