我可以从循环的父模板中获取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)
答案 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 }}
请注意,您可能实际上并不意味着从父模板扩展子模板,因此我在此示例中省略了它。