from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Электронный адрес'}))
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'password1', 'password2')
# clean email field
def clean_email(self):
email = self.cleaned_data["email"]
try:
User.objects.get(email=email)
except User.DoesNotExist:
return email
raise forms.ValidationError('Такой адрес электронной почты уже зарегестрирован.')
# modify save() method so that we can set user.is_active to False when we first create our user
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.username = self.cleaned_data['email']
user.email = self.cleaned_data['email']
if commit:
user.is_active = False # not active until he opens activation link
user.save()
return user