【问题标题】:why does <myproject>/accounts/profile/ show the <myproject>/profile/ page为什么 <myproject>/accounts/profile/ 显示 <myproject>/profile/ 页面
【发布时间】:2019-12-29 18:24:16
【问题描述】:

使用 django-allauth,成功登录后,用户被重定向到 http://&lt;myproject&gt;/accounts/profile/... 但是这个 URL 不存在,但它仍然成功显示来自 http://&lt;myproject&gt;/profile/ 的视图

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/ 视图...

【问题讨论】:

    标签: django django-urls django-allauth


    【解决方案1】:

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

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

    LOGIN_REDIRECT_URL = '/profile/'
    

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

    【讨论】:

    • 对。我明白那个。但我没有看到我在任何 urls.py 中实现该视图的位置。所以这就是为什么我对渲染该视图的原因感到困惑
    • 是重定向到/profile/ 还是在/accounts/profile/ 上呈现与/profile/ 相同的页面?
    • 显示的 url 是 /accounts/profile。所以不是重定向,而是渲染
    【解决方案2】:

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

        url('profile/', include('user_profile.urls')),
    

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

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

    那么只有profile/ 会匹配。

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2019-09-09
      • 2019-01-06
      • 2018-10-13
      • 2018-01-28
      • 2016-11-04
      • 1970-01-01
      • 2013-05-06
      • 2012-10-22
      相关资源
      最近更新 更多