我的根URLconf模块中有以下内容(还有更多,但并不重要,因此请忽略):
urlpatterns = [
re_path(r'^password-reset-redirect-view/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
password_reset_redirect,
name = 'password_reset_confirm'),
path('', include('search.urls')),
path('', include('customer_portal.urls')),
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
这是 customer_portal.urls :
urlpatterns = [
path('customer/contact/', views.contact),
path('', views.home),
re_path(r"^confirm-email/(?P<key>[-:\w]+)/$", views.email_verification,
name="account_confirm_email"),
]
这是 rest_auth.registration.urls :
urlpatterns = [
url(r'^$', RegisterView.as_view(), name='rest_register'),
url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'),
url(r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
name='account_confirm_email'),
]
您可以看到两个包含的urls。pyurlpattern具有一个名为“ account_confirm_email”的视图。
在代码中的某处运行了
url = reverse(
"account_confirm_email",
args=[emailconfirmation.key])
由于customer_portal.urls
之前包含rest_auth.registration.urls
,因此我希望上述account_confirm_email
方法返回customer_portal.urls
中的路由reverse
。但是,我却得到了rest_auth.registration.urls
路线URL。
请确保已注释掉rest_auth.registration.urls
中的路由,然后确实获得了返回的正确URL(customer_portal URL)。
它已填充到电子邮件中,我检查该电子邮件并看到我想要的URL:http://127.0.0.1:8000/confirm-email/......./
,而不是:http://127.0.0.1:8000/rest-auth/registration/account-confirm-email/...../
有人能告诉我为什么在两种情况下customer_portal
URL都不被反向吗?
Django文档说:
Django按顺序运行每个URL模式,并在与请求的URL匹配的第一个URL模式处停止。