Tuesday, 20 August 2013

TypeError when tried to override the UserCreationForm's.error message

TypeError when tried to override the UserCreationForm's.error message

I have a registration form from UserCreationForm, to which I had override
with the following:
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
import re
from django.core.exceptions import ObjectDoesNotExist
class UserCreationForm(UserCreationForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'username',)
error_messages = {
'duplicate_username': _("A user with that email already exists."),
'password_mismatch': _("The two password fields didn't match."),
}
username = forms.EmailField(label='Email', max_length=250, unique=True)
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.email = user.username
user.save()
return user
But I was getting an error:
NameError at /admin/
name '_' is not defined
So, I had to import
from django.utils.translation import gettext as _
And that solved that problem. But now, after that has been fixed (i
guess..that was the solution for the first problem), I am getting another
error:
TypeError at /admin/
__init__() got an unexpected keyword argument 'unique'
If I remove 'unique' from the EmailField, everything works fine. So, do I
remove the unique=true from the form? Will it always be unique for each
username(here its email), even though I remove it? And one more thing, was
from django.utils.translation import gettext as _ the suited solution for
the error name '_' is not defined??? I am a newbie in django. Any help
will be greatly appreciated! Thank you.

No comments:

Post a Comment