我正在尝试将一些CSS数据添加到我创建的内联formset中。我之前已经完成了常规ModelForms
,但它不适用于内联formset。
以下是自定义__init__
:
class CustomSubIFormset(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super(CustomSubIFormset, self).__init__(*args, **kwargs)
self.forms.widget.attrs.update({'placeholder': 'User Name'})
for name, forms in self.forms.items():
forms.widget.attrs.update({'class': 'form-control'})
views.py
subIFormSet = inlineformset_factory(Study, SubInvestigator, fields=('name',), extra=4, formset=CustomSubIFormset)
错误消息是'list'
对象没有属性'widget'
。追溯:
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/RickyD/PycharmProjects/StudyTrack/studies/views.py" in create_study
63. formset = subIFormSet(instance=Study())
File "/Users/RickyD/PycharmProjects/StudyTrack/studies/forms.py" in __init__
56. self.forms.widget.attrs.update({'placeholder': 'User Name'})
Exception Type: AttributeError at /studies/create-study/
Exception Value: 'list' object has no attribute 'widget'
答案 0 :(得分:0)
请改为尝试:
for form in self.forms:
for field in form.fields:
field.widget.attrs.update({'class': 'form-control'})