0

我可以从父模板 forloop 中获取 forloop 值吗?IE:

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传递参数

父.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 回答