Advertisement
DiegoDG01

Untitled

Apr 7th, 2023
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. from django.contrib.auth.forms import SetPasswordForm
  2.  
  3. class UserSetPasswordForm(SetPasswordForm):
  4.     password = forms.CharField(label=_("Contraseña"), widget=forms.PasswordInput)
  5.     confirm_password = forms.CharField(label=_("Confirmar contraseña"), widget=forms.PasswordInput)
  6.  
  7.     def __init__(self, *args, **kwargs):
  8.         super(UserSetPasswordForm, self).__init__(*args, **kwargs)
  9.         self.fields['password'].widget.attrs.update({'class': 'form-control form-control-lg bg-gray-800 border-dark'})
  10.         self.fields['confirm_password'].widget.attrs.update({'class': 'form-control form-control-lg bg-gray-800 border-dark'})
  11.         # add fields to the form
  12.         self.fields['password'].label = "Contraseña"
  13.         self.fields['confirm_password'].label = "Confirmar contraseña"
  14.  
  15.     def clean(self):
  16.         cleaned_data = super().clean()
  17.         password = cleaned_data.get("password")
  18.         confirm_password = cleaned_data.get("confirm_password")
  19.         if password != confirm_password:
  20.             raise forms.ValidationError("Las contraseñas no coinciden")
  21.         return cleaned_data
  22.  
  23.     def save(self, commit=True):
  24.         user = super().save(commit=False)
  25.         user.set_password(self.cleaned_data["password"])
  26.         if commit:
  27.             user.save()
  28.         return user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement