Django:使用父模板中的forloop值

时间:2013-03-02 21:34:11

标签: django django-templates

我可以从循环的父模板中获取for循环值吗?如果:

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)

1 个答案:

答案 0 :(得分:1)

include模板标记支持使用with keyword传递参数。

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 }}

请注意,您可能实际上并不意味着从父模板扩展子模板,因此我在此示例中省略了它。

相关问题