Skip to content

Instantly share code, notes, and snippets.

@jonas-grgt
Created October 16, 2012 20:37
Show Gist options
  • Save jonas-grgt/3901823 to your computer and use it in GitHub Desktop.
Save jonas-grgt/3901823 to your computer and use it in GitHub Desktop.
signupform
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