Skip to content

Instantly share code, notes, and snippets.

@dickbrouwer
Created August 31, 2010 11:23
Show Gist options
  • Save dickbrouwer/558883 to your computer and use it in GitHub Desktop.
Save dickbrouwer/558883 to your computer and use it in GitHub Desktop.
class EmailUserCreationForm(UserCreationForm):
'''
User login form class that replaces the 'username' field with
an 'emailfield' on registration, sets email = username in the
User object, and adds case-insensitive username verification.
'''
username = forms.EmailField(label=("Email Address"))
confirm = forms.BooleanField(error_messages={'required':
'Please read and accept the Terms and Conditions to continue'})
def clean_username(self):
username = self.cleaned_data["username"]
try:
User.objects.get(username__iexact=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(_("A user with that username already exists."))
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.email = user.username
user.save()
return user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment