gpt4 book ai didi

python - 如何将 attr 传递给 forms.CharField

转载 作者:太空宇宙 更新时间:2023-11-04 01:53:44 26 4
gpt4 key购买 nike

我想将一些 Html 属性传递给 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'

最佳答案

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

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

关于python - 如何将 attr 传递给 forms.CharField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57398766/

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