-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
26 lines (23 loc) · 1.04 KB
/
forms.py
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
from django import forms
from django.contrib.auth.models import User
class RegisterForm(forms.Form):
username = forms.CharField(label='Username', max_length=100)
email = forms.EmailField(label='Email')
password = forms.CharField(widget=forms.PasswordInput)
password2 = forms.CharField(widget=forms.PasswordInput, label='Confirm password')
def clean_username(self):
username = self.cleaned_data.get('username')
if User.objects.filter(username=username).exists():
raise forms.ValidationError('Username is already taken')
return username
def clean_email(self):
email = self.cleaned_data.get('email')
if User.objects.filter(email=email).exists():
raise forms.ValidationError('Email is already taken')
return email
def clean(self):
cleaned_data = super().clean()
password = cleaned_data.get('password')
password2 = cleaned_data.get('password2')
if password != password2:
raise forms.ValidationError('Passwords do not match')