当试图覆盖UserCreationForm时,TypeError [英] TypeError when tried to override the UserCreationForm
问题描述
from django.contrib.auth我已经使用UserCreationForm的注册表单从django.contrib.auth.models导入的.forms import UserCreationForm
从django导入的用户
import form
import d
from django.core.exceptions import ObjectDoesNotExist
class UserCreationForm(UserCreationForm):
class Meta:
model = User
fields =('first_name','last_name','username'
error_messages = {
'duplicate_username':_(具有该电子邮件的用户已存在),
'password_mismatch':_(两个密码字段didn t匹配),
}
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.sa ve()
返回用户
但是我收到一个错误:
Name / at / admin /
名称'_'未定义
所以,我不得不从django.utils.translation导入
导入gettext为_
这解决了这个问题。但是现在,在此之后,我已经修复了(我猜这是第一个问题的解决方案),我收到另一个错误:
TypeError at / admin /
__init __()得到一个意想不到的关键字参数'unique'
如果我从 EmailField
中删除'unique',一切都可以正常运行。那么,我从表单中删除 unique = true
?对于每个用户名(这里是其电子邮件),它始终是唯一的,即使我删除它吗?另外还有一件事是从django.utils.translation导入gettext作为_ 的错误名称
'_'的适用解决方案未定义
???我是django的新手。任何帮助将不胜感激!谢谢。
unique
不是 forms.EmailField
。你猜我的模型字段令我感到困惑。
你想使用电子邮件字段的用户名吗?如果是这样,请尝试以下方式:
class UserCreationForm(UserCreationForm):
username = forms.EmailField(widget = forms.TextInput(attrs = {'maxlength':75}),
label = _(Email))
至于您的name_'的查询没有定义:
from django。 utils.translation import ugettext_lazy as _
这是从django.contrib.auth.forms代码。 p>
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.
unique
is not a parameter for forms.EmailField
. You are confusing it with model field I guess.
And you want to use email field for username? If thats the case, try this:
class UserCreationForm(UserCreationForm):
username = forms.EmailField(widget=forms.TextInput(attrs={'maxlength':75}),
label=_("Email"))
And as to your query for "name '_' is not defined" :
from django.utils.translation import ugettext_lazy as _
This is from django.contrib.auth.forms code.
这篇关于当试图覆盖UserCreationForm时,TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!