Created
October 16, 2012 20:37
-
-
Save jonas-grgt/3901823 to your computer and use it in GitHub Desktop.
signupform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.contrib.auth.models import User | |
from django.contrib.auth.forms import UserCreationForm | |
from django.utils.translation import ugettext as _ | |
class SignupForm(UserCreationForm): | |
def __init__(self, *args, **kwargs): | |
super(SignupForm, self).__init__(*args, **kwargs) | |
self.fields.pop('username') | |
email = forms.EmailField(required=True) | |
class Meta: | |
model = User | |
fields = ("email", "password1", "password2",) | |
def clean_email(self): | |
email = self.cleaned_data['email'] | |
try: | |
user = User.objects.get(email=email) | |
raise forms.ValidationError(_("Email already exists.")) | |
except User.DoesNotExist: | |
return email | |
return email | |
def save(self, commit=True): | |
user = super(SignupForm, self).save(commit=False) | |
user.email = self.cleaned_data["email"] | |
user.username = self.cleaned_data["email"] | |
if commit: | |
user.save() | |
return user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment