Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample Django registration form
class RegistrationForm(forms.Form):
username = forms.CharField(
label='Имя пользователя',
max_length=30,
widget=forms.TextInput(attrs={'class':'uname-reg'}))
email = forms.EmailField(
label='Email',
widget=forms.EmailInput( attrs={'class' : 'email-reg'})
)
password1 = forms.CharField(
label='Пароль',
widget=forms.PasswordInput(attrs={'class': 'pswd-reg'})
)
password2 = forms.CharField(
label='Пароль (еще раз)',
widget=forms.PasswordInput(attrs={'class': 'pswd-reg'})
)
captcha = CaptchaField()
def clean_password2(self):
if 'password1' in self.cleaned_data:
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 == password2:
return password2
raise forms.ValidationError('Пароли не соответствуют друг другу')
def clean_username(self):
username = self.cleaned_data['username']
if not re.search(r'^\w+$', username):
raise forms.ValidationError(
'Имя пользователя может содеожать лишь буквы, цифры и знак подчеркивания'
)
try:
User.objects.get(username=username)
except ObjectDoesNotExist:
return username
raise forms.ValidationError('Пользователь с таким именем уже существует. Пожалуйста выберите другое имя.')
def clean_email(self):
email = None
try:
email = self.cleaned_data['email']
User.objects.get(email=email)
raise forms.ValidationError('Этот email уже используется')
except User.DoesNotExist:
return email
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.