Add custom behavior to PasswordResetView?


I have successfully implemented password reset functionality using the built-in views from from django.contrib.auth.views. But I need to add a bit of extra functionality to this flow. Our site has audit log for extra security and so I would like to log when user resets their password.

Ideally this would be part of successful POST in PasswordResetView but I have no idea how to add that. I tried looking at the documentation but could not find what specifically to override. I am mostly using FBVs so I guess that is part of my problem right now :slight_smile:

Ideally if I could create custom PasswordResetView subclass, override the rendering method where I would add the audit log entry and then used super implementation to carry on.

Thanks for help!


I haven’t implemented what you describe myself, but here’s a tip that might help:
When I want to figure out what method to override I usually go to the excellent site, e. g. It’s great for getting an overview of a CBV’s methods, and what arguments each method expects.

In your case, you want to add code that is to be run if there was a successful POST request, i. e. the form data were valid. It seems to make sense then to override the form_valid method, e. g.

# inside of your View
def form_valid(self, form):
    # logging code, probably making use of data bound to self.request.user
    return super().form_valid(form)

Note that I haven’t tried this myself, but I think this would be a viable method.

(“ccbv” stands for " Classy Class-Based Views", which helps remembering the URL)


Thanks! That is exactly what I wanted. Works great.

I have came across the site previously but I feel like it is really aimed at quite experience folks since it “just” lists method without more details.

Actually, no? Most of the various components on that site are clickable, even down to the source code level, allowing you to see exactly what’s going on.

It’s really an amazing and amazingly useful site.

You are right. I somehow managed to miss this.