Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django modelform how to add a confirm password field?

Tags:

Here I need to add an extra confirmation password in my form.I used Django's modelform. I also need to validate both passwords. It must raise a validation error if password1 != password2.

Here is my forms.py:

class UserForm(forms.ModelForm):     password=forms.CharField(widget=forms.PasswordInput())      class Meta:         model=User         fields=('username','email','password')  class UserProfileForm(forms.ModelForm):     YESNO_CHOICES = (('male', 'male'), ('female', 'female'))     sex = forms.TypedChoiceField(choices=YESNO_CHOICES, widget=forms.RadioSelect)     FAVORITE_COLORS_CHOICES=(('red','red'),('blue','blue'))     favorite_colors = forms.MultipleChoiceField(required=False,widget=forms.CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)     dob = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'),                                   input_formats=('%d/%m/%Y',))      class Meta:          model=UserProfile         fields=('phone','picture','sex','favorite_colors','dob') 

And here is my registration function:

def register(request):     registered = False     if request.method == 'POST':         user_form = UserForm(data=request.POST)         profile_form = UserProfileForm(data=request.POST)            if user_form.is_valid() and profile_form.is_valid():             user = user_form.save(commit=False)             user.set_password(user.password)             user.save()             profile = profile_form.save(commit=False)             profile.user = user             if 'picture' in request.FILES:                 profile.picture = request.FILES['picture']             profile.save()             registered = True         else:             print user_form.errors, profile_form.errors     else:         user_form = UserForm()         profile_form = UserProfileForm()      return render(request,             'mysite/register.html',             {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} ) 
like image 969
sunnysm Avatar asked Jan 05 '16 10:01

sunnysm


People also ask

Is there any password field in Django?

The Django's Forms The above form has two inputs - a text field named username (the name attribute in the html input field is what determines the name of input field) and a password field named password - and a submit button. The form uses POST method to submit form data to server.

How does Django validate password?

validate(self, password, user=None) : validate a password. Return None if the password is valid, or raise a ValidationError with an error message if the password is not valid. You must be able to deal with user being None - if that means your validator can't run, return None for no error.


2 Answers

Use clean like

class UserForm(forms.ModelForm):     password=forms.CharField(widget=forms.PasswordInput())     confirm_password=forms.CharField(widget=forms.PasswordInput())     class Meta:         model=User         fields=('username','email','password')      def clean(self):         cleaned_data = super(UserForm, self).clean()         password = cleaned_data.get("password")         confirm_password = cleaned_data.get("confirm_password")          if password != confirm_password:             raise forms.ValidationError(                 "password and confirm_password does not match"             ) 
like image 145
itzMEonTV Avatar answered Sep 28 '22 06:09

itzMEonTV


def clean(self):     cleaned_data = super(UserAccountForm, self).clean()     password = cleaned_data.get("password")     confirm_password = cleaned_data.get("confirm_password")      if password != confirm_password:         self.add_error('confirm_password', "Password does not match")      return cleaned_data 
like image 42
Savai Maheshwari Avatar answered Sep 28 '22 04:09

Savai Maheshwari