DJANGO 2.0自定义URL正则义务



我有一个URL模式,例如

url(r'^reset/(?P<uidb64>[0-9A-Za-z_-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html'),

我如何将其用作新的path模式

django.contrib.auth.urls中的视图作为

包含
path('reset/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),

使用re_path代替url。

from django.url import re_path
re_path(r'^reset/(?P<uidb64>[0-9A-Za-z_-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html'),

https://docs.djangoproject.com/en/2.0/ref/urls/

您可以使用新的路径模式使用正则:re_path()。如果您只想使用path()创建自己的自定义转换器,例如:

class TokenConverter:
    regex = '[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})'
    def to_python(self, value):
        #code here
    def to_url(self, value):
        #code here

然后您可以在UrlConf

中使用它

最新更新