【问题标题】:How to use the attrs in forms.CharField(widget=forms.PasswordInput()如何使用 forms.CharField(widget=forms.PasswordInput() 中的属性
【发布时间】:2017-10-01 10:16:50
【问题描述】:

我正在尝试在 Django 中创建自定义字段 我不确定这是不是正确的方法,但我不断收到一个我无法理解的错误enter code here

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"})),

    }

【问题讨论】:

    标签: css django forms widget


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多