【问题标题】:Give custom ids to formset with django crispy forms使用 django 脆皮表单为表单集提供自定义 ID
【发布时间】:2015-03-19 06:54:04
【问题描述】:

我想使用表单集为带有 Crispy 表单的 DIV 提供自定义 ID。 我正在尝试在表单布局中使用 forloop.counter,因为它说它将被注入模板渲染但它不起作用(它只是被渲染为 css_id='liveprice_{{forloop.counter}}'

class PrinterMaterialForm(ModelForm):
    class Meta:
        model = PrinterMaterial
        fields = ('material', 'hour_cost', 'gram_cost', 'colors')

    def __init__(self, *args, **kwargs):
        super(PrinterMaterialForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.form_class = 'form-inline'
        self.helper.field_template = 'bootstrap3/layout/inline_field.html'
        self.helper.disable_csrf = True
        self.helper.layout = Layout(
            Div(
                Div(
                    Div(css_class='col-md-4', css_id='liveprice_{{ forloop.counter }}'),
                    css_class='row',
                ),
            ),
        )

【问题讨论】:

    标签: django django-crispy-forms


    【解决方案1】:

    我找到了解决方案。 实际上,django 清晰的表单足够智能,可以在注入的上下文中模拟 {{ forloop }}。

    问题是它只渲染字段、HTML 等节点,而不渲染 css_class、css_id(属性)。

    因此我使用的解决方案如下:

    self.helper.layout = Layout(
        Div(
            Div(
                HTML("<div class='col-md-4' id='liveprice_{{ forloop.counter }}'></div>"),,
                css_class='row',
            ),
        ),
    )
    

    我希望它可以帮助某人..

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2020-08-21
      • 2021-12-12
      • 1970-01-01
      • 2014-02-28
      • 2015-10-08
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多