0

親テンプレートのforloopからforloop値を取得できますか?すなわち:

parent.html
{% extends 'base.html' %}
{% block content %}
    {% for i in nnn %}
        {% include child.html %}
    {% endfor %}
{% end block %}

child.html
{% extends 'base.html' %}
{% block content %}
    {{ forloop.counter from parent.html }}
{% endblock %}

def ViewParent(request):
    return render_to_response('parent.html', {}, context_instance)
4

1 に答える 1

1

テンプレートタグは、キーワードincludeを使用した引数の受け渡しをサポートします。with

parent.html:

{% extends 'base.html' %}
{% block content %}
    {% for i in nnn %}
        {% include child.html with loop_counter=forloop.counter %}
    {% endfor %}
{% end block %}

child.html:

{{ loop_counter }}

親と同じベーステンプレートから子テンプレートを拡張することを実際に意味するわけではないことに注意してください。そのため、この例ではそれを省略しています。

于 2013-03-02T22:32:39.587 に答える