如何从 Django 中的 UserCreationForm 中删除用户名字段
我希望我的用户注册页面显示电子邮件和密码字段,而不显示用户名。 我已经创建了此注册表:
class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
#fullname = forms.CharField(label = "First name")
class Meta:
model = User
fields = ("email", )
def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
但用户名仍然出现。我需要覆盖其他东西吗?
I want my user registration page to display email and password field, and no username.
I have created this Register Form:
class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
#fullname = forms.CharField(label = "First name")
class Meta:
model = User
fields = ("email", )
def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
But the username still appears. Do I need to override something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从表单字段中弹出用户名,如下所示:
但是,您需要在保存之前填写一些随机用户名,如下所示:
当您以这种方式破解时,还需要考虑其他事项,例如确保您的 LoginForm 会拉出稍后需要时输入用户名:
You can pop out the username from the form's fields like so:
But then you would need to fill-in some random username before saving like so:
There are other considerations to take when you hack it this way, like making sure your LoginForm would pull the username later on when it is needed:
找到了。这正是我想做的:
http://www.micahcarrick.com/django-email-authentication.html
Found it. This is exactly what I wanted to do:
http://www.micahcarrick.com/django-email-authentication.html
将该字段添加到 Meta 类的排除中:
Add that field to the Meta class's exclude: