class RegistrationForm forms Form username UsernameField email forms E

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class RegistrationForm(forms.Form):
username = UsernameField()
email = forms.EmailField(label=_('Email'))
password = PasswordField(label=_('Password'))
password_dup = PasswordField(label=_('Password (confirmation)'))
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
def clean_email(self):
try:
User.objects.get(email__exact=self.cleaned_data['email'])
except User.DoesNotExist:
return self.cleaned_data['email']
except KeyError:
pass
else:
raise forms.ValidationError(_(u'This email already registered'))
def clean(self):
pwd1 = self.cleaned_data.get('password')
pwd2 = self.cleaned_data.get('password_dup')
if pwd1 and pwd2:
if pwd1 != pwd2:
self._errors['password_dup'] = [_('Passwords do not match')]
return self.cleaned_data
def save(self):
username = self.cleaned_data['username']
email = self.cleaned_data['email']
password = self.cleaned_data['password']
user = User.objects.create_user(username, email, password=password)
return user