首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >django模板中的嵌套for循环没有显示正确的输出

django模板中的嵌套for循环没有显示正确的输出
EN

Stack Overflow用户
提问于 2017-06-28 13:13:30
回答 1查看 850关注 0票数 1

所以我有一个循环,当我在我的views.py文件中打印它时,它显示了正确的输出。

代码语言:javascript
运行
复制
for x in list4:
        print x[0]
        for y in x[3]:
            print y[1]
        print "\n"

输出:

代码语言:javascript
运行
复制
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代码

代码语言:javascript
运行
复制
{% 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 %} 

模板输出

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-29 04:09:08

正如@alfonso.kim所指出的,问题在于注释,因为django仍然认为注释for循环是一个实际的循环,因此删除

代码语言:javascript
运行
复制
<!-- {% for b in y %} -->
<!-- {% endfor %} -->

密码很好用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44803548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档