我需要从我的 Form
中呈现各个字段,所以我使用 crispy-forms
中的过滤器 |as_crispy_field
并使用 bootstrap 3 作为样式,但我需要在 form-inline
中完成它,就像这里的第一个例子 bootstrap example page所以我试着这样做:
template.html
<form class="form-inline">{% csrf_token %}
{{ form.name|as_crispy_field }}
</form>
但标签显示在 TextInput 字段上方,而不是我需要的内联。 我如何使用 |as_crispy_field 做到这一点?
编辑:这是用 crispy 渲染后的 HTML
<form class="form-inline"><input type='hidden' name='csrfmiddlewaretoken' value='****...' />
<div id="div_id_name" class="form-group">
<label for="id_name" class="control-label requiredField">
Name<span class="asteriskField">*</span>
</label>
<div class="controls ">
<input class="form-control textinput textInput form-control" id="id_name" maxlength="100" name="name" type="text" />
</div>
</div>
</form>
最佳答案
您链接到的标签位于字段左侧,在 Crispy 中称为“水平表格”。是explained here .您需要设置这些 helper
属性
helper.form_class = 'form-horizontal'
helper.label_class = 'col-lg-2'
helper.field_class = 'col-lg-8'
实际的内联表单,标签显示在字段内,就在同一页面的正下方。你只需要设置两个辅助属性
helper.form_class = 'form-inline'
helper.field_template = 'bootstrap3/layout/inline_field.html'
如果我正确理解您的问题,就应该这样做。
关于django - 使用 'as_crispy_field' 在表单内联中呈现单个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36292416/