【问题标题】:How to change css_class for all <div> rows in Crispy form如何更改 Crispy 表单中所有 <div> 行的 css_class
【发布时间】:2022-12-29 08:59:30
【问题描述】:

我正在使用 Crispy 表单来呈现我的 Django 表单。它工作顺利,但我很难为我的行更新 css_class。任何建议如何解决这个问题?表单是从 forms.models 中检索的,因此理想情况下我不必为每一行单独更新布局。

文档https://django-crispy-forms.readthedocs.io/en/latest/layouts.html#overriding-layout-objects-templates 没有回答我的问题(或者我没看懂)


html 中的当前输出

<div id="div_id_voornaam" class="form-group row"> </div>

理想情况下:为每一行更改 css 类:

<div id="div_id_voornaam" class="row mb-3"> </div>

表单设置

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_class = 'form-horizontal'
    self.helper.label_class = 'col-lg-2'
    self.helper.field_class = 'col-lg-8'

模板设置

<div class="card-body">
    <form method="post">
        {% load crispy_forms_tags %}

        {% csrf_token %}
        {% crispy form %}

        <button type="submit" value="Submit" class="btn btn-primary">Submit</button>
    </form>

【问题讨论】:

    标签: django-forms django-templates django-crispy-forms


    【解决方案1】:

    您需要使用 wrapper_css 属性。像这样: ...self.helper.layout = Layout(Field('voornaam', wrapper_class="row mb-3"))

    你可以在这里查看 crispy 文档:https://django-crispy-forms.readthedocs.io/en/latest/layouts.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2017-03-08
      • 1970-01-01
      • 2015-11-23
      • 2017-01-10
      相关资源
      最近更新 更多