我有一个来自 UserCreationForm 的注册表单,我用以下内容覆盖了该注册表单:
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
但我收到一个错误:
NameError at /admin/
name '_' is not defined
所以,我不得不导入
from django.utils.translation import gettext as _
这解决了这个问题。但是现在,在修复之后(我猜..这是第一个问题的解决方案),我得到了另一个错误:
TypeError at /admin/
__init__() got an unexpected keyword argument 'unique'
如果我从 'unique' 中删除EmailField
,一切正常。那么,我要从unique=true
表格中删除吗?即使我删除了每个用户名(这里是它的电子邮件),它是否总是唯一的?还有一件事,是from django.utils.translation import gettext as _
错误名称的合适解决方案'_' is not defined
吗???我是 django 的新手。任何帮助将不胜感激!谢谢你。