2016-01-05 20 views
5

Formumda fazladan bir confirmation password eklemem gerekiyor. Django'nun model formunu kullandım. Ayrıca her iki şifreyi de doğrulamam gerekiyor. password1 != password2 ise bir doğrulama hatası oluşturmalıdır. İşte Django modeli, onaylama parola alanı nasıl eklenir?

benim forms.py geçerli:

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') 

Ve işte benim kayıt fonksiyonudur:

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}) 

cevap

7

Kullanım clean

gibi
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" 
      ) 
1

forms.py için bu deneyin:

class UserForm(forms.Form): 
    password = forms.CharField(widget=forms.PasswordInput()) 
    password_confirm = forms.CharField(widget=forms.PasswordInput()) 

    class Meta: 
     model = User 
     fields=('username','email','password') 

Ve görünümlerde bu .py:

if user_form.is_valid() and profile_form.is_valid() and user_form.cleaned_data['password'] == user_form.cleaned_data['password_confirm']: 
    ... 
elif user_form.data['password'] != user_form.data['password_confirm']: 
    user_form.add_error('password_confirm', 'The passwords do not match') 
0
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