django - 使用 'as_crispy_field' 在表单内联中呈现单个字段

标签 django django-crispy-forms django-filters

我需要从我的 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/

相关文章:

Django:Rest Framework 验证 header

Django Crispy Forms - 通过助手添加按钮

python - 具有多个引用的 django 列表模型条目

Python:如何加入运算符?

python - Django的objects.filter, "expensive"怎么会是这样?

python - 将子域包含到 Django 站点地图 url 的正确方法是什么?

python - 为什么通过ip访问可以,但是域访问失败?

django - 如何将 Django forms.ChoiceField 呈现为 Twitter Bootstrap 下拉菜单

python - Django 上的 Crispy Form VariableDoesNotExist

django - 将 Django 过滤器集应用于带注释的查询集?