gpt4 book ai didi

css - 如何使用 forms.CharField(widget=forms.PasswordInput() 中的 attrs

转载 作者:行者123 更新时间:2023-12-02 03:24:34 25 4
gpt4 key购买 nike

我正在尝试在 Django 中创建自定义字段我不确定这是否是正确的方法,但我不断收到无法理解的错误在此处输入代码

AttributeError: 'CharField' object has no attribute 'attrs'

这是我的代码。谁能解释一下这是怎么回事?

class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
confirm_password = forms.CharField(widget=forms.PasswordInput())

class Meta:
model = User
fields = ('username', 'email', 'password', 'confirm_password')

widgets = {
'username': forms.TextInput(attrs={'class': "form-control input-lg",
'placeholder': "نام کاربری *",
'name': 'display_name',
'required': "required",
'tabindex': "3",
'data-error': "password is required"}),
'email': forms.EmailInput(attrs={'class': "form-control input-lg",
'placeholder': 'ایمیل *',
'name': 'email',
'required': "required",
'tabindex': "4",
'data-error': "email is required"}),
'confirm_password': forms.CharField(widget=forms.PasswordInput(attrs={'class': "form-control input-lg",
'placeholder': "تکرار رمز *",
'name': 'confirm_password',
'required': "required",
'tabindex': "6",
'data-error': "confirm password is required"})),
'password': forms.CharField(widget=forms.PasswordInput(attrs={"class": "form-control input-lg",
'placeholder': " رمز *",
'name': 'password',
'required': "required",
'tabindex': "5",
'data-error': " password is required"})),

}

最佳答案

您没有正确使用widgets参数。您为确认密码密码设置的值在它们应该是小部件时是字段。您应该直接使用 forms.PasswordInput

如果您想自定义用于表单字段的类,而不是从模型中指定的类派生,则需要提供 field_classes 属性。

关于css - 如何使用 forms.CharField(widget=forms.PasswordInput() 中的 attrs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43762471/

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