gpt4 book ai didi

python - Django:向 UserCreationForm 添加其他字段

转载 作者:行者123 更新时间:2023-12-02 16:32:33 25 4
gpt4 key购买 nike

我们可以在 Django 中向 UserCreationForm 添加额外的字段吗?

默认情况下,UserCreationForm 中有 5 个字段:

  1. 用户名
  2. 电子邮件
  3. 名字
  4. 姓氏
  5. 密码

如果我想添加其他字段,如年龄、性别,那么我该如何在 UserCreationForm 中添加这些字段。

我是 django 的新手,任何引用或描述性代码都将非常有用。

最佳答案

按照 Link 执行此操作

class SignUpForm(UserCreationForm):
# My Own Custom Fields
username = forms.CharField(forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Username'}))
first_name = forms.CharField(forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'First Name'}), max_length=32, help_text='First name')
last_name=forms.CharField(forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Last Name'}), max_length=32, help_text='Last name')
email=forms.EmailField(forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'Email'}), max_length=64, help_text='Enter a valid email address')
password1=forms.CharField(forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'}))
password2=forms.CharField(forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password Again'}))

# The Default Fields Of The UserCreation Form
class Meta(UserCreationForm.Meta):
model = User
# I've tried both of these 'fields' declaration, result is the same
# fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )
fields = UserCreationForm.Meta.fields + ('first_name', 'last_name', 'email',)

关于python - Django:向 UserCreationForm 添加其他字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63277469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com