Can I add more fields to django password reset forms? For example, username fields: then if username and email is valid, the password reset link will be sent to email. Please help me, I want to password reset like this photoenter image description here
Asked
Active
Viewed 149 times
0
-
yes , why not. control is totally up to you. – Shakil Mar 17 '19 at 16:16
2 Answers
0
Yes, you can. The form can be customized as you wish.
You can find more information in the documentation and in this question.

Yennefer
- 5,704
- 7
- 31
- 44
0
You Can Customize the PasswordResetForm. To add username in PasswordResetForm customize as below.
add the mentioned code to customize to you needs. look at #add this tags in code
import unicodedata
#add this UsernameField
class UsernameField(forms.CharField):
def to_python(self, value):
return unicodedata.normalize("NFKC", super().to_python(value))
def widget_attrs(self, widget):
return {
**super().widget_attrs(widget),
"autocapitalize": "none",
"autocomplete": "username",
}
class PasswordResetForm(forms.Form):
#add this
username = UsernameField(
widget=forms.TextInput(attrs={"autofocus": True})
)
email = forms.EmailField(
label=_("Email"),
max_length=254,
widget=forms.EmailInput(attrs={"autocomplete": "email"}),
)
...
#code as in PasswordResetForm
....
email = self.cleaned_data["email"]
username = self.cleaned_data["username"] #add this
if not domain_override:
current_site = get_current_site(request)
site_name = current_site.name
domain = current_site.domain
else:
site_name = domain = domain_override
email_field_name = UserModel.get_email_field_name()
for user in self.get_users(email):
if user.username == username: #add this
user_email = getattr(user, email_field_name)
context = {
"email": user_email,
"domain": domain,
"site_name": site_name,
"uid": urlsafe_base64_encode(force_bytes(user.pk)),
"user": user,
"token": token_generator.make_token(user),
"protocol": "https" if use_https else "http",
**(extra_email_context or {}),
}
self.send_mail(
subject_template_name,
email_template_name,
context,
from_email,
user_email,
html_email_template_name=html_email_template_name,
)

Dharman
- 30,962
- 25
- 85
- 135