Customize PasswordResetConfirmView

Viewed 930

I'm trying to set a user is_active state to True when the user have clicked on the reset-password link I've emailed them. However, I don't understand how to get access to the PasswordResetConfirmView, the code below don't do any prints when I go to the related URL.

Any ideas on how I should do this?

I've read the source code over here: https://github.com/django/django/blob/main/django/contrib/auth/views.py

But I don't really understand how I should "intercept" the view and add the operations I want to do and then let it keep going.

from django.contrib.auth import views as auth_views

     
   class PasswordResetConfirmView(auth_views.PasswordResetConfirmView):
              
        print("Request view")
        
        def get(self, request, *args, **kwargs):
            print(request)
            print(request.user)
            return super().get(request, *args, **kwargs)

This is the related URL

    path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="main/password/password_reset_confirm.html"), name='password_reset_confirm'),

EDIT: Also tried adjusting the loginview, but got the same problem. I think I'm mistunderstand how things work behind the hood.

class LoginUser(auth_views.LoginView):
print("test")
def get(self, request, *args, **kwargs):
    print("test")
    return super(LoginUser, self).get(request, *args, **kwargs)

def get_success_url(self):
    return resolve_url('accounts:login')
1 Answers

You can override the SetPasswordForm, and activate the user object:

from django.contrib.auth.forms import SetPasswordForm

class MySetPasswordForm(SetPasswordForm):
    
    def save(self, *args, commit=True, **kwargs):
        user = super().save(*args, commit=False, **kwargs)
        user.is_active = True
        if commit:
            user.save()
        return user

Then we can use this as our MySetPasswordForm:

path(
    'reset/<uidb64>/<token>/',
    views.PasswordResetConfirmView.as_view(form_class=MySetPasswordForm),
    name='password_reset_confirm'
),
Related