django 模板中的嵌套 for 循环未显示正确的输出
Nested for loop in django template not showing correct output
所以我有这个循环,当我在我的views.py文件
中打印它时,它显示了正确的输出
for x in list4:
print x[0]
for y in x[3]:
print y[1]
print "\n"
输出:
test question edited
test1
test2
test3
test question
hello
hello12
hkjhjkh
hjk
hkjhkj
another test
sdfsdf
sdfsdf
test2
sdfsd
sdfsd
another
sdfsdf
fsdfsd
sdf
sdfsd
fsd
但是当 运行 django 模板中的相同循环以显示重复输出的形式显示值时
test.jinja2代码
{% extends "base.jinja2" %}
{% block content %}
{% block body %}
{% for x in ques %}
<form class='form-horizontal' method=POST action="/test-portal/student/test/">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<div class="form-group">
<input type="hidden" name="id" value={{x.0}}>
<label for="ques_title" class="col-sm-2 control-label" name='ques_title'>{{x[0]}}</label>
</div>
{% for y in x[3] %}
<!-- {% for b in y %} -->
<div class="form-group">
<div class="col-sm-2">
<!-- <input type='checkbox' name='flag' id="new" value={{x}}> -->
<label for="option" class="col-sm-2 control-label" name='ques_title'>{{y[1]}}</label>
</div>
</div>
<!-- {% endfor %} -->
{% endfor %}
{% endfor %}
<div class="form-group">
<button type="submit" class="btn btn-default" name='button' value='submit'>SUBMIT</button>
<!-- <td><button type="submit" class="btn btn-default" name='button' value='options'>ADD OPTIONS</button></td> -->
</div>
</form>
{% endblock %}
{% endblock %}
模板输出
test question edited
test1
test1
test1
test1
test2
test2
test2
test2
test3
test3
test3
test3
test question
hello
hello
hello
hello
hello12
hello12
hello12
hello12
hkjhjkh
hkjhjkh
hkjhjkh
hkjhjkh
hjk
hjk
hjk
hjk
hkjhkj
hkjhkj
hkjhkj
hkjhkj
another test
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
test2
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
another
sdfsdf
sdfsdf
sdfsdf
sdfsdf
fsdfsd
fsdfsd
fsdfsd
fsdfsd
sdf
sdf
sdf
sdf
sdfsd
sdfsd
sdfsd
sdfsd
fsd
fsd
fsd
fsd
如@alfonso.kim所述,问题出在注释上,因为 django 仍在考虑注释的 for 循环是一个实际循环,因此通过删除
<!-- {% for b in y %} -->
<!-- {% endfor %} -->
代码运行良好。
所以我有这个循环,当我在我的views.py文件
中打印它时,它显示了正确的输出for x in list4:
print x[0]
for y in x[3]:
print y[1]
print "\n"
输出:
test question edited
test1
test2
test3
test question
hello
hello12
hkjhjkh
hjk
hkjhkj
another test
sdfsdf
sdfsdf
test2
sdfsd
sdfsd
another
sdfsdf
fsdfsd
sdf
sdfsd
fsd
但是当 运行 django 模板中的相同循环以显示重复输出的形式显示值时
test.jinja2代码
{% extends "base.jinja2" %}
{% block content %}
{% block body %}
{% for x in ques %}
<form class='form-horizontal' method=POST action="/test-portal/student/test/">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<div class="form-group">
<input type="hidden" name="id" value={{x.0}}>
<label for="ques_title" class="col-sm-2 control-label" name='ques_title'>{{x[0]}}</label>
</div>
{% for y in x[3] %}
<!-- {% for b in y %} -->
<div class="form-group">
<div class="col-sm-2">
<!-- <input type='checkbox' name='flag' id="new" value={{x}}> -->
<label for="option" class="col-sm-2 control-label" name='ques_title'>{{y[1]}}</label>
</div>
</div>
<!-- {% endfor %} -->
{% endfor %}
{% endfor %}
<div class="form-group">
<button type="submit" class="btn btn-default" name='button' value='submit'>SUBMIT</button>
<!-- <td><button type="submit" class="btn btn-default" name='button' value='options'>ADD OPTIONS</button></td> -->
</div>
</form>
{% endblock %}
{% endblock %}
模板输出
test question edited
test1
test1
test1
test1
test2
test2
test2
test2
test3
test3
test3
test3
test question
hello
hello
hello
hello
hello12
hello12
hello12
hello12
hkjhjkh
hkjhjkh
hkjhjkh
hkjhjkh
hjk
hjk
hjk
hjk
hkjhkj
hkjhkj
hkjhkj
hkjhkj
another test
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
sdfsdf
test2
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
sdfsd
another
sdfsdf
sdfsdf
sdfsdf
sdfsdf
fsdfsd
fsdfsd
fsdfsd
fsdfsd
sdf
sdf
sdf
sdf
sdfsd
sdfsd
sdfsd
sdfsd
fsd
fsd
fsd
fsd
如@alfonso.kim所述,问题出在注释上,因为 django 仍在考虑注释的 for 循环是一个实际循环,因此通过删除
<!-- {% for b in y %} -->
<!-- {% endfor %} -->
代码运行良好。