2010-03-14 6 views
1

어떤 신체든지 장고의 두 필드를 비교할 수있는 방법에 대한 아이디어를 제안 할 수 있습니다. 나는 내 forms.py 파일에 두 개의 암호 필드가 있습니다. 이제 두 필드를 비교하고 둘 다 동일하면 데이터베이스에 사용자를 저장하십시오. 오류 메시지를 추가하여 값을 다시 입력하십시오.django의 두 필드를 비교

감사

+3

http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other –

+0

http : //pypi.python. org/pypi/django-registration/0.7 –

답변

2
는 양식의 clean 메서드를 재정의

:

class MyRegistrationForm(forms.Form): 
    password1=... 
    password2=... 
    ... 

def clean(self): 
    cleaned_data = self.cleaned_data # individual field's clean methods have already been called 
    password1 = cleaned_data.get("password1") 
    password2 = cleaned_data.get("password2") 
    if password1 != password2: 
     raise forms.ValidationError("Passwords must be identical.") 

    return cleaned_data 

더 많은 정보를 원하시면 the docs를 참조하십시오.

또한 클라이언트 측에서 이것을 확인하기 위해 일부 자바 스크립트를 추가해야합니다. 클라이언트 측 유효성 검사는 서버 측 유효성 검사를위한 보조 도구가 아니지만 사용자 응답 성이 뛰어나고 대역폭을 절약합니다.