使用过滤器django的多个表单

时间:2016-02-27 22:09:02

标签: django django-forms django-filter

我想在模板中显示具有相同过滤器的多个表单。

是否可以做这样的事情?

@register.filter
def formLightWithColor(phase,color):

    lightParameter = LightParameter.objects.filter(phase = phase).filter(color = color).all()
    all_params = None

    if lightParameter != None:
        for lights_params in lightParameter:
            all_params = ManageLight(instance=lights_params, prefix = lights_params.id).as_p()
        return all_params
    else:
        return None

在模板中:

{{ phase|formLightWithColor:color }}

谢谢!

1 个答案:

答案 0 :(得分:0)

我做了这个并且有效:

@register.filter
def formLightWithColor(phase,color):
    lightParameter = LightParameter.objects.filter(phase=phase).filter(color=color).all()
    all_params = []
    if lightParameter != None:
        for lights_params in lightParameter:
            all_params.append(ManageLight(instance=lights_params, prefix=lights_params.id).as_p())
        return all_params
    else:
        return None

在模板中:

{% for light_form in phase|formLightWithColor:color %}
    {{ light_form }}
{% endfor %}