【问题标题】:how to pass attr to forms.CharField如何将 attr 传递给 forms.CharField
【发布时间】:2019-12-15 08:21:54
【问题描述】:

我想将一些 Html attr 传递给由crispy 生成的表单字段

我在 django 2.2.3 中尝试使用此代码


class AddForm(forms.Form):
    todo = forms.CharField(max_length=128, attrs={'placeholder':'What do you want to do?'})

但我遇到了一个错误

    from .forms import AddForm
  File "C:\Users\HP\django_project\todo\forms.py", line 4, in <module>
    class AddForm(forms.Form):
  File "C:\Users\HP\django_project\todo\forms.py", line 5, in AddForm
    todo = forms.CharField(max_length=128, attrs={'placeholder':'What do you want to do?'})
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\fields.py", line 214, in __init__
    super().__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'attrs'

【问题讨论】:

  • 使用 widget=forms.TextInput(attrs={'placeholder': '你想做什么?'})

标签: python django django-forms django-crispy-forms


【解决方案1】:

attrs 是小部件的参数,而不是字段。

todo = forms.CharField(max_length=128, widget=forms.TextInput(attrs={'placeholder':'What do you want to do?'}))

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 2021-09-08
    • 2016-05-13
    • 1970-01-01
    • 2020-05-07
    • 2012-10-22
    • 2020-06-30
    • 2014-07-15
    相关资源
    最近更新 更多