Django-allauth:success_url 的 PasswordChangeView 覆盖注销用户导致错误
Django-allauth: PasswordChangeView override of success_url with logged out user results in error
通过 django-allauth 更改密码时,成功发布密码更改后的默认重定向再次是密码更改模板。因为我觉得这很混乱,所以我覆盖了 views.py 文件中的原始 PasswordChnageView:
from allauth.account.views import PasswordChangeView
from django.urls import reverse_lazy
class MyPasswordChangeView(PasswordChangeView):
success_url = reverse_lazy('home')
并更改了我的 urls.py 文件:
from django.urls import path, include
from users.views import MyPasswordChangeView
urlpatterns = [
...
# User management
path('accounts/password/change/', MyPasswordChangeView.as_view(), name="account_change_password"),
path('accounts/', include('allauth.urls')),
...
]
这在用户登录时工作正常,但是当我在注销时尝试访问 url http://127.0.0.1:8000/accounts/password/change/ 时,我收到以下错误消息:AttributeError at /accounts/password/change/ 'AnonymousUser' object has no attribute 'has_usable_password'
在我创建自定义覆盖之前,相同行为的结果是我被重定向到登录 url http://127.0.0.1:8000/accounts/login/?next=/
当注销的用户试图访问 url http://127.0.0.1:8000/accounts/password/change/
时,我需要对我的自定义视图进行哪些更改才能重定向到登录 url
查看源码:allauth的PasswordChangeView
本身没有login required装饰器,直接在urls中添加:使用的视图是password_change = login_required(PasswordChangeView.as_view())
.
有两种方法:
- 将 login_required 装饰器添加到您的 URL。
from django.contrib.auth.decorators import login_required
path('accounts/password/change/', <b>login_required(MyPasswordChangeView.as_view())</b>, name="account_change_password"),
from django.contrib.auth.mixins import LoginRequiredMixin
class MyPasswordChangeView(<b>LoginRequiredMixin</b>, PasswordChangeView):
success_url = reverse_lazy('home')
确保 LoginRequiredMixin 位于 child class.
的最左侧
通过 django-allauth 更改密码时,成功发布密码更改后的默认重定向再次是密码更改模板。因为我觉得这很混乱,所以我覆盖了 views.py 文件中的原始 PasswordChnageView:
from allauth.account.views import PasswordChangeView
from django.urls import reverse_lazy
class MyPasswordChangeView(PasswordChangeView):
success_url = reverse_lazy('home')
并更改了我的 urls.py 文件:
from django.urls import path, include
from users.views import MyPasswordChangeView
urlpatterns = [
...
# User management
path('accounts/password/change/', MyPasswordChangeView.as_view(), name="account_change_password"),
path('accounts/', include('allauth.urls')),
...
]
这在用户登录时工作正常,但是当我在注销时尝试访问 url http://127.0.0.1:8000/accounts/password/change/ 时,我收到以下错误消息:AttributeError at /accounts/password/change/ 'AnonymousUser' object has no attribute 'has_usable_password'
在我创建自定义覆盖之前,相同行为的结果是我被重定向到登录 url http://127.0.0.1:8000/accounts/login/?next=/
当注销的用户试图访问 url http://127.0.0.1:8000/accounts/password/change/
时,我需要对我的自定义视图进行哪些更改才能重定向到登录 url查看源码:allauth的PasswordChangeView
本身没有login required装饰器,直接在urls中添加:使用的视图是password_change = login_required(PasswordChangeView.as_view())
.
有两种方法:
- 将 login_required 装饰器添加到您的 URL。
from django.contrib.auth.decorators import login_required
path('accounts/password/change/', <b>login_required(MyPasswordChangeView.as_view())</b>, name="account_change_password"),
from django.contrib.auth.mixins import LoginRequiredMixin
class MyPasswordChangeView(<b>LoginRequiredMixin</b>, PasswordChangeView):
success_url = reverse_lazy('home')
确保 LoginRequiredMixin 位于 child class.
的最左侧