在PasswordChangeView中实现“ LOGOUT”功能的适当位置在哪里?

时间:2019-04-07 07:43:00

标签: django django-views django-authentication

你好Stackoverflow社区。

在PasswordChangeView中实现LOGOUT功能的最佳位置在哪里

我尝试了


class PasswordCorrectionView(SuccessMessageMixin, LoginRequiredMixin, PasswordChangeView):
    template_name = "admin/password_change.html”
    form_class = PwdChgForm

    def post(self, request, *args, **kwargs):
        logout(request)   # here
        return PasswordChangeView.post(self, request, *args, **kwargs)

但它上升了:

NotImplementedError at /account/password/change
Django doesn't provide a DB representation for AnonymousUser.

这很合理,因为我还是无法保存AnonymousUser密码。

所以问题是哪种方法最好在PasswordChangeView中覆盖?

或者第二个选项覆盖表格中的某些方法:


class PwdChgForm(PasswordChangeForm):

    def save(self, commit=True):
        self.user.is_activated = False
        user_registrated.send(PwdChgForm, instance=self.user) # signal to the email sender
        PasswordChangeForm.save(self, commit=True)

用户需要更改密码后才能注销(然后通过电子邮件等确认)。除注销外,所有其他工作

1 个答案:

答案 0 :(得分:1)

更改此

def post(self, request, *args, **kwargs):
        logout(request)   # here
        return PasswordChangeView.post(self, request, *args, **kwargs)

def post(self, request, *args, **kwargs):
    PasswordChangeView.post(self, request, *args, **kwargs)
    logout(request)   # here
    return redirect('your-login-url')