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

发布于 2024-12-05 20:07:48 字数 521 浏览 1 评论 0原文

我希望我的用户注册页面显示电子邮件和密码字段,而不显示用户名。 我已经创建了此注册表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

川水往事 2024-12-12 20:07:48

您可以从表单字段中弹出用户名,如下所示:

class RegisterForm(UserCreationForm):

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

但是,您需要在保存之前填写一些随机用户名,如下所示:

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()

当您以这种方式破解时,还需要考虑其他事项,例如确保您的 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()

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:

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()
郁金香雨 2024-12-12 20:07:48

将该字段添加到 Meta 类的排除中:

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

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

Add that field to the Meta class's exclude:

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

    class Meta:
        model = User
        exclude = ['username',]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文