为什么 <myproject>/accounts/profile/ 显示 <myproject>/profile/ 页面

why does <myproject>/accounts/profile/ show the <myproject>/profile/ page

使用 django-allauth,在成功登录后用户被重定向到 http://<myproject>/accounts/profile/... 然而这个 URL 不存在,但它仍然成功显示来自 [=18 的视图=]

settings.py

urlpatterns = [
    path('', include('pages.urls')),
    path('admin/', admin.site.urls),
    url(r'^accounts/', include('allauth.urls')),
    url('album/', include('albums.urls')),
    url('profile/', include('user_profile.urls')),
]

user_profile\urls.py

urlpatterns = [
    path('', views.profile, name='profile'),
]

使用 show_urls 我没有看到任何 /accounts/* 会调用 view.profile 视图

/accounts/confirm-email/        allauth.account.views.EmailVerificationSentView account_email_verification_sent
/accounts/confirm-email/<key>/  allauth.account.views.ConfirmEmailView  account_confirm_email
/accounts/email/        allauth.account.views.EmailView account_email
/accounts/inactive/     allauth.account.views.AccountInactiveView       account_inactive
/accounts/login/        allauth.account.views.LoginView account_login
/accounts/logout/       allauth.account.views.LogoutView        account_logout
/accounts/password/change/      allauth.account.views.PasswordChangeView        account_change_password
/accounts/password/reset/       allauth.account.views.PasswordResetView account_reset_password
/accounts/password/reset/done/  allauth.account.views.PasswordResetDoneView     account_reset_password_done
/accounts/password/reset/key/<uidb36>-<key>/    allauth.account.views.PasswordResetFromKeyView  account_reset_password_from_key
/accounts/password/reset/key/done/      allauth.account.views.PasswordResetFromKeyDoneView      account_reset_password_from_key_done
/accounts/password/set/ allauth.account.views.PasswordSetView   account_set_password
/accounts/signup/       allauth.account.views.SignupView        account_signup
/accounts/social/connections/   allauth.socialaccount.views.ConnectionsView     socialaccount_connections
/accounts/social/login/cancelled/       allauth.socialaccount.views.LoginCancelledView  socialaccount_login_cancelled
/accounts/social/login/error/   allauth.socialaccount.views.LoginErrorView      socialaccount_login_error
/accounts/social/signup/        allauth.socialaccount.views.SignupView  socialaccount_signup

只有实际的 /profile/ url...

/profile/       user_profile.views.profile      profile

帮助我理解为什么 /accounts/profile/ 显示 /profile/ 视图...

实际上重定向到 /accounts/profile/ 是 django 中的默认行为。在django 全局设置中,它被定义为LOGIN_REDIRECT_URL。 Django 希望你实现这个 page/url。 Django django-allauth 也使用相同的设置在登录后重定向用户。

如果您想重定向到任何其他页面,例如 /profile/,请在您的项目设置中覆盖此设置。

LOGIN_REDIRECT_URL = '/profile/'

或者,如果您希望 django-allauth 完全不重定向,请按照 here 所述在您的设置中设置 LOGIN_REDIRECT_URL = False

您的 profile url 路径不限于仅匹配 url:

的开头
    url('profile/', include('user_profile.urls')),

这将匹配 gibberishprofile/ 之类的任何内容,例如 accounts/profile/。如果将 url 配置更改为

    url('^profile/', include('user_profile.urls')),  # note the ^ before profile

那么只有 profile/ 会匹配。