自定义url模式之前的Django第三方库url模式? (网址格式顺序)

时间:2019-04-01 21:53:57

标签: python django django-rest-framework django-rest-auth

使用Django(REST Framework),这是我的根网址conf:

... (imports)

urlpatterns = [
    re_path(r"^confirm-email/(?P<key>[-:\w]+)/$", accounts_views.email_verification,
            name="account_confirm_email"),
    path('admin/', admin.site.urls),
    path('request/', include('request.urls')),
    path('rest-auth/', include('rest_auth.urls')),
    path('rest-auth/registration/', include('rest_auth.registration.urls')),
    re_path(r'^account-confirm-email/', TemplateView.as_view(),
            name='account_email_verification_sent'),
]

现在,我正在使用 django-rest-auth 库进行身份验证。该库在用户提交注册表格后向用户发送电子邮件,以激活用户的电子邮件地址。

此电子邮件中的链接是通过反转网址格式获得的,该网址格式名为: account_confirm_email

django-rest-auth库带有它自己的名为 account_confirm_email 的网址格式,确切的说,在以下文件中:

python3.7 / site-packages / rest_auth / registration / urls.py:

... (imports)

urlpatterns = [
    ...
    url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
        name='account_confirm_email'),
]

我希望我自己的url模式是可逆的,而不是rest-auth,因为我的顺序是第一位的。如Django文档所述:

  

Django按顺序运行每个URL模式,并在第一个停止   与请求的网址匹配的一个。

https://docs.djangoproject.com/en/2.2/topics/http/urls/

但是在实践中,rest-auth模式是被反转的,为什么会发生这种情况?

为完整起见,我看到Django文档也说:

  

Django确定要使用的根URLconf模块。通常这是   ROOT_URLCONF设置的值,但如果传入HttpRequest   对象具有urlconf属性(由中间件设置),其值将为   代替ROOT_URLCONF设置。

https://docs.djangoproject.com/en/2.2/topics/http/urls/

django-rest-auth可以执行以上Django文档报价中所述的内容吗?如果是这样,是否仍然有可能在django-rest-auth的模式之前颠倒我自己的url模式? (那我该怎么做?)

1 个答案:

答案 0 :(得分:1)

应该会这样,因为您编写的URLPattern与通过 django-rest-auth 库发送的电子邮件中存在的URL不匹配。 / p>

替换此行:

re_path(r"^confirm-email/(?P<key>[-:\w]+)/$", accounts_views.email_verification, name="account_confirm_email"),

为此:

re_path(r"^account-confirm-email/(?P<key>[-:\w]+)/$", accounts_views.email_verification, name="account_confirm_email"),