[英]Django render a form field with css using crispy forms
I have a form I want to render with crispy form, 我有一个要用酥脆的形式呈现的表格,
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>
The fields are not rendered with the class form-control
. 字段不使用class form-control
呈现。 The labels are rendered and I don't need them since I have my custom labels. 标签已呈现,因为我有自定义标签,所以不需要它们。 Any help on how I can render the fields with the class will be highly appreciated. 我将如何在类中渲染字段提供任何帮助,我们将不胜感激。 Thanks. 谢谢。
If you render your fields manually, you can add them like: 如果您手动呈现字段,则可以像这样添加它们:
<input type="text" name="{{ form.objective.html_name }}"
id={{ form.objective.id_for_label }}"
class="form-control">
Please note that this will require you to render all your input
s manually. 请注意,这将要求您手动呈现所有input
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.