Django:使用Foreignkey表单遍历Jinja“包含”标签

时间:2018-12-27 10:08:01

标签: inheritance django-forms jinja2 multiple-inheritance

我有一个继承自主要形式的子形式,并且我设置了包含用于不同形式形式的标签。对于简单表单或复杂表单,我的“ include”文件夹具有所有基于Jinja的逻辑。现在,我需要使用include标记,并需要使用Foreignkey遍历子表单字段。但是我做不到。小小的帮助将不胜感激。

继承的表单:

class InheritedAddressForm(AddressForm):

def __init__(self, *args, **kwargs):
    super(InheritedAddressForm, self).__init__(*args, **kwargs)
    self.fields['extended_form_value'].queryset = InheritedModel.objects.all()

表单包含代码段:includes / forms / simple_input_field.html

{% load widget_tweaks %}
<div class="{% if field.errors %}has-error{% endif %}">

    {% if not nolabel and field|widget_type != 'checkboxinput' and field|widget_type != 'radioselect' and field|widget_type != 'hiddeninput' %}
        <label for="{{ field.auto_id }}" class="control-label{% if field.field.required %} required{% endif %}">
            {{ field.label|safe }}
        </label>
    {% endif %}
    {% if field|widget_type == 'checkboxinput' %}
        <div class="custom-control custom-checkbox">
            {% if field.errors %}
                {% render_field field class+="custom-control-input is-invalid" id=field.auto_id %}
            {% else %}
                {% render_field field class+="custom-control-input" id=field.auto_id %}
            {% endif %}
            {% if not nolabel %}
                <label for="{{ field.auto_id }}" class="custom-control-label">
                    <span class="{% if field.field.required %}required{% endif %}">{{ field.label|safe }}</span>
                </label>
            {% endif %}
        </div>
    {% elif field|widget_type == 'radioselect' %}
        {% if not nolabel %}
            <div class="control-label">
                <span class="{% if field.field.required %}required{% endif %}">{{ field.label|safe }}</span>
            </div>
        {% endif %}
        <div class="input-list-container inline">
            {% for choice in field %}
                <label class="custom-control custom-radio">
                    <input class="custom-control-input" id="{{choice.id_for_label}}" type="radio" value="{{choice.choice_value}}" name="{{field.name}}"{% if field.value == choice.choice_value %} checked="checked"{% endif %}>
                    <label class="custom-control-label" for="{{choice.id_for_label}}">{{choice.choice_label}}</label>
                </label>
            {% endfor %}
        </div>
    {% else %}
        {% if field.errors %}
            {% if field|widget_type == 'select' or field|widget_type == 'advancedselect' %}
                {% render_field field class+="custom-select is-invalid" %}
            {% else %}
                {% render_field field class+="form-control is-invalid " %}
            {% endif %}
        {% else %}
            {% if field|widget_type == 'select' or field|widget_type == 'advancedselect' %}
                {% render_field field class+="custom-select" %}
            {% else %}
                {% render_field field class+="form-control" %}
            {% endif %}
        {% endif %}
    {% endif %}

    {% if not noerror %}
        {% for error in field.errors %}
            <span class="error-block"><i class="icon-exclamation-sign"></i> {{ error }}</span>
        {% endfor %}
    {% endif %}

    {% if field.help_text %}
        <span class='help-block'>
            {# We allow HTML within form help fields #}
            {{ field.help_text|safe }}
        </span>
    {% endif %}
</div>

我继承的表单:

    {% if form.extended %}
    <div class="form-row">
            <div class="form-col-full form-group">
                #{% for choice in extended_set.all %}
                {% include 'includes/forms/simple_input_field.html' with field=extended %}
#Need to iterate extended with "include" tag. 
                #{% endfor %}
        </div>
    {% endif %}

我出现了扩展名,但是我需要遍历扩展名以在模板中获得带有扩展名的ForeignKey值。有人可以建议一种如何使用include标记进行迭代的方法吗?

0 个答案:

没有答案