0

Hello Stackoverflow community.

Where is the best place to implement LOGOUT function in PasswordChangeView

I tried


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)

but it rises:

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

That's reasonable cos I cant save AnonymousUser password anyway.

So question is what method is the best one to override here in PasswordChangeView???

Or second option override some method in the forms:


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)

I need user to logout after he has chaged his password (then confirm it via email, etc) All this work except LOGOUT

user3837868
  • 917
  • 1
  • 12
  • 24
Aleksei Khatkevich
  • 1,923
  • 2
  • 10
  • 27

1 Answers1

1

Change this

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

to

def post(self, request, *args, **kwargs):
    PasswordChangeView.post(self, request, *args, **kwargs)
    logout(request)   # here
    return redirect('your-login-url')
shafik
  • 6,098
  • 5
  • 32
  • 50