Django 添加 CSS 到 InlineFormSet

Django Add CSS to InlineFormSet

我正在尝试将一些 CSS 数据添加到我创建的内联表单集中。我以前对常规 ModelForms 做过,但它不适用于内联表单集。

这里是习俗__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'

试试这个:

for form in self.forms:
   for field in form.fields:
      field.widget.attrs.update({'class': 'form-control'})