简体   繁体   English

如何从Django中的UserCreationForm中删除用户名字段

[英]How to remove the Username field from the UserCreationForm in Django

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? 我需要覆盖其他内容吗?

You can pop out the username from the form's fields like so: 您可以从表单的字段中弹出用户名,如下所示:

class RegisterForm(UserCreationForm):

    def __init__(self, *args, **kwargs): 
        super(RegisterForm, self).__init__(*args, **kwargs) 
        # remove username
        self.fields.pop('username')
    ...

But then you would need to fill-in some random username before saving like so: 但是之后你需要在保存之前填写一些随机用户名:

from random import choice
from string import letters
...

class RegisterForm(UserCreationForm):
...
    def save(self):
        random = ''.join([choice(letters) for i in xrange(30)])
        self.instance.username = random
        return super(RegisterForm, self).save()

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: 当您以这种方式破解它时还需要考虑其他因素,例如确保您的LoginForm稍后在需要时提取用户名:

class LoginForm(AuthenticationForm):

    email = forms.EmailField(label=_("E-mail"), max_length=75)

    def __init__(self, *args, **kwargs): 
        super(LoginForm, self).__init__(*args, **kwargs) 
        self.fields['email'].required = True 
        # remove username
        self.fields.pop('username')

    def clean(self):
        user = User.objects.get(email=self.cleaned_data.get('email'))
        self.cleaned_data['username'] = user.username
        return super(LoginForm, self).clean()

Found it. 找到了。 This is exactly what I wanted to do: http://www.micahcarrick.com/django-email-authentication.html 这正是我想要做的: http//www.micahcarrick.com/django-email-authentication.html

Add that field to the Meta class's exclude: 将该字段添加到Meta类的排除:

class RegisterForm(UserCreationForm):
    email = forms.EmailField(label = "Email")
    #fullname = forms.CharField(label = "First name")

    class Meta:
        model = User
        exclude = ['username',]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django如何从UserCreationForm中删除密码字段 - Django How To remove the password field from the UserCreationForm 如何从 Django 的 UserCreationForm 中的用户名字段禁用自动对焦? - How to disable autofocus from username field in Django's UserCreationForm? 如何删除 django UserCreationForm 中的密码确认字段 - How to remove password confirmation field in django UserCreationForm 如何在 Django 的 UserCreationForm 中扩展字段? - How to extend field in UserCreationForm in django? UserCreationForm没有字段用户名 - UserCreationForm has no field username 当我在 django 中创建 usercreationform 时,用户名字段是自动选择的,我该如何修复它 - When i create usercreationform in django the username field is auto selected and how can i fix it 为什么UserCreationForm在Django的模型类中定义了UserName字段明确? - why is the UserCreationForm has UserName field explicity defined in model class in Django? 从 django UserCreationForm 中删除 help_text - Remove help_text from django UserCreationForm 从 django-allauth 中删除“用户名”字段 - Remove 'username' field from django-allauth 如何在Django的UserCreationForm / User Model中将客户的电子邮件作为用户名? - How to make customer's email as the username in UserCreationForm / User Model in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM