2
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'), 

I've been doing tutorial but in django 2.1 you have to use path, how do I translate to 2.1 django compatibile path function?

Does the

path('activate/<str:uidb64>/<uuid:token>/', views.activate, name='activate') 

do the same?

1

1 Answer 1

6

I've been doing tutorial but in django 2.1 you have to use path, how do I translate to 2.1 django compatibile path function?

No, in , you can use path [Django-doc] or re_path [Django-doc]. Furthermore as of today, url [Django-doc] is still supported, but will likely dissapear in the future.

re_path is in fact equivalent to the old url, so you can write this as:

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' ),

It is not easy to construct a completely equivalent URL, since Django only supports five path conversions by default:

Path converters

The following path converters are available by default:

  1. str - Matches any non-empty string, excluding the path separator, '/'. This is the default if a converter isn't included in the expression.
  2. int - Matches zero or any positive integer. Returns an int.
  3. slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.
  4. uuid - Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.
  5. path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.

We can use slug here, but this will match more than the given URL:

path( r'^activate/(<slug:uidb64>/<slug:token>/$', views.activate, name='activate' ),

The slug pattern takes as regex equivalent:

class SlugConverter(StringConverter): regex = '[-a-zA-Z0-9_]+' 
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, that's what I've been looking for - thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.