简体   繁体   English

Django Crispy Form loop through {% crispy %} object

[英]Django Crispy Form loop through {% crispy %} object

I'm trying to use crispy form fields within each table's column.我正在尝试在每个表的列中使用清晰的表单字段。 I can render it using:我可以使用以下方法渲染它:

{% load crispy_forms_tags %}
<tr>
    <form method="get" class="form-inline justify-content-center">
        {% for field in filter.form %}
            <th>{{ field|as_crispy_field }}</th>
        {% endfor %}
        <input class='hidden-submit' type="submit"/>
    </form>
</tr>

And it looks like this which is what I want:它看起来像这样,这就是我想要的:

在此处输入图像描述

But the problem is my Layout() which I'm using to add extra parameters such has placeholders etc to the form is not working because I'm using |as_crispy_field tag to render individual fields.但问题是我用来向表单添加额外参数(如占位符等)的Layout()不起作用,因为我正在使用|as_crispy_field标记来呈现单个字段。 Here is my form component:这是我的表单组件:

class CustomFiltersForm(forms.Form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_show_labels = False
        self.helper.form_tag = False
        self.helper.layout = Layout(

        )

        for field_name, field in self.fields.items():
            self.helper.layout.append(Field(field_name, placeholder="Search " + field.label))

Is there a way to loop through {% crispy filter.form %} or an alternative way to populate individual fields using crispy forms?有没有办法循环通过{% crispy filter.form %}或使用脆 forms 填充单个字段的替代方法? Something like:就像是:

{% for field in {% crispy filter.form%} %}
...
{% endfor %}

You are trying to do two different things, either you render the fields manually or you render them through the layout option on the crispy form helper.您正在尝试做两件不同的事情,要么手动渲染字段,要么通过脆表单助手上的布局选项渲染它们。 I would ditch the individual field approach and just go with the layout option that way the placeholders and multiple attributes you add will be visible.我会放弃单独的字段方法,只使用带有布局选项的 go,这样您添加的占位符和多个属性将可见。

Forms should look something like this. Forms 应该看起来像这样。

class CustomFiltersForm(forms.Form): class CustomFiltersForm(forms.Form):

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_show_labels = False
    self.helper.form_tag = False

    for field_name, field in self.fields.items():
        self.helper.layout.append(Column(Field(field_name, placeholder="Search " + field.label)))
        self.helper.layout.append(Hidden(Submit("submit"))

Template should look something like this:模板应如下所示:

{% load crispy_forms_tags %}
<tr>
    <form method="get" class="form-inline justify-content-center">
        {% crispy filter_form %}
    </form>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM