Django将CSS添加到InlineFormSet

时间:2016-01-28 03:19:31

标签: python css django inline-formset

我正在尝试将一些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'

1 个答案:

答案 0 :(得分:0)

请改为尝试:

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