我有一个表格,我想以清脆的形式呈现,
FORM
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
excludes = ('user', 'created')
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_show_labels = False ## I only need the input field
self.helper.layout = layout.Layout(
layout.Field('name', css_class="form-control"),
layout.Field('objective', css_class="form-control"),
## ADD CLASS form-control TO THE FIELDS (This is the problem since the fields are not rendered with the class)
)
super(MyForm, self).__init__(*args, **kwargs)
TEMPLATE
<div class="form-group">
<label>Name <i class="fa fa-question-circle tooltips" data-original-title="Lorem Ipsum" data-html="true" data-placement="right" data-container="body"></i></label>
{{ form.name|as_crispy_field }}
{{ form.name.errors|striptags }}
</div>
<div class="form-group">
<label>Theme/ Objective <i class="fa fa-question-circle tooltips" data-original-title="Lorem Ipsum" data-html="true" data-placement="right" data-container="body"></i></label>
{{ form.objective|as_crispy_field }}
{{ form.objective.errors|striptags }}
</div>
不会使用类form-control
呈现字段。
标签已呈现,我不需要它们,因为我有自定义标签。
任何有关如何使用该类渲染字段的帮助将受到高度赞赏。感谢。
答案 0 :(得分:1)
如果手动渲染字段,可以添加如下字段:
<input type="text" name="{{ form.objective.html_name }}"
id={{ form.objective.id_for_label }}"
class="form-control">
请注意,这需要您手动呈现所有input
。