【问题标题】:Where is the proper place to implement "LOGOUT" function in PasswordChangeView?在 PasswordChangeView 中实现“LOGOUT”功能的合适位置在哪里?
【发布时间】:2019-04-07 07:43:00
【问题描述】:

你好 Stackoverflow 社区。​​p>

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)

我需要用户在他更改密码后注销(然后通过电子邮件等确认)所有这些工作除了注销之外

【问题讨论】:

    标签: django django-views django-authentication


    【解决方案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')
    

    【讨论】:

    • @Shafikur Ra​​hman。谢谢,它确实有效。只需要 HttpResponseRedirect 而不是重定向
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    相关资源
    最近更新 更多