我有一個非常簡單的形式:自定義表單驗證
from django import forms
class InitialSignupForm(forms.Form):
email = forms.EmailField()
password = forms.CharField(max_length=255, widget = forms.PasswordInput)
password_repeat = forms.CharField(max_length=255, widget = forms.PasswordInput)
def clean_message(self):
email = self.clean_data.get('email', '')
password = self.clean_data.get('password', '')
password_repeat = self.clean_data.get('password_repeat', '')
try:
User.objects.get(email_address = email)
raise forms.ValidationError("Email taken.")
except User.DoesNotExist:
pass
if password != password_repeat:
raise forms.ValidationError("The passwords don't match.")
請問這是怎麼自定義窗體驗證呢?我需要評估email
目前沒有用戶使用該電子郵件地址。我還需要評估password
和password_repeat
的匹配。我怎麼能這樣做呢?
我寧願就mothel而不是形式的檢驗:http://beutil.blogspot.com/2011/10/django-moving- business-rules-from-views.html – danihp