Hello. I want to customise the default Django password reset view in order to allow users to enter their username or email, and then receive an email with a reset link.
Does anybody know how to implement this functionality, as I have tried by inheriting the Password Reset form and password reset view, however there is very little documentation on this. Here are the two files I need to edit in order to implement this. Also on the form, I only want one field, where a user can enter their username or email.
Can anybody point me in the right direction, i.e which function to edit, and where to add the necessary code. Thanks.
Well, in a way it is - a good starting point anyway.
You’re not going to “edit” any system files, you’re going to build your functionality on top of those functions.
For example, you specify that you want to customize the password reset form.
Looking at the documentation for PasswordResetView, it says (in part):
template_name : The full name of a template to use for displaying the password reset form. Defaults to registration/password_reset_form.html if not supplied.
Notice that the directory is just specified as “registration/password_reset_form.html”. If you read the Templates documentation, you’ll see that Django will search multiple locations for a file. So to override the default template, you can create a registration directory within yourtemplates directory, and that will be the template that is rendered.
However, you’re also looking at changing functionality, so that means you’re looking to alter the view as well.
So, overall, you’re looking at:
Creating your custom password_reset_form.html template.
Since you want the user to be able to enter either username or email in that one field, you’ll probably want to override the get_users method for your form. The supplied get_users function assumes an email address was entered. You might want to write your version to check to see if an email was entered. If not, check to see if a username was entered and find the corresponding email address(es) for that username. Look at the get_users method in PasswordResetForm to see how the provided method does this for some ideas.
Specify the form_class attribute for your custom form.
Creating a url for your “password reset view”
If you’re creating a new template for your form, you might also want to create a similar template for the PasswordResetDone view in registration/password_reset_done.html
Linking your page to your custom view rather than the default url.
(I know that sometimes this feels like you’re touching lots of different things - and that’s ok. You’re making small changes to bits and pieces rather than needing to write a whole lot of code yourself.)
@KenWhitesell Thanks man, helps a lot, but when you mean override the get_users method, do you mean inherit from existing PasswordResetView and make a function with the name get_users to override this function from the parent class? And by inheriting, all the other functionality for the view will still be implemented right ? Am I correct here? Thanks.
First, for clarity and accuracy, it’s the get_users in the PasswordResetForm, not PasswordResetView.
But yes, something like (short version, not complete):
def get_users(self, username_or_email):
Yes, you are correct. By inheriting from a parent class, all functions and attributes will apply, except for those overridden by the child class.
Since you’re inheriting from PasswordResetForm, all other attributes and functions from it will apply. Off the top of my head, I don’t see where anything else would be needed here (I’m far from sure about that, but that’s my gut hunch at the moment).