Django内置模板标签不起作用

时间:2015-10-29 13:06:42

标签: django django-templates

这是我在模板中使用的html代码。

    <html>
    <head>
    <title>{{usn}}</title>
    </head>
    <body>
    <h1>USN {{usn}}</h1>
    <h2>Name {{name}}</h2>
        <h3>{{department}}</h3>
        <% if department %>
        <h4>{{department}}</h4>

        <table>

            <ul>
                <% for r in result %>
                <li>{{result}}</li>
                < endfor %>
            </ul>
            <% for r in result %>
            <li>{{result}}</li>
            <!--<tr><td>r[0][2]</td><td>r[0][3]</td><td>r[0][4]</td></tr>-->
            <% endfor %>
        </table>
    </body>
</html>

而不是for循环执行,我发现它按原样打印语句。 Screenshot of output

我主要需要遍历变量,并以表格格式打印其值。所以请帮助我,关于如何让for循环执行。感谢。

1 个答案:

答案 0 :(得分:2)

您的django模板语法错误:https://docs.djangoproject.com/en/1.8/topics/templates/#tags

  

标签被{%和%}

包围

(不是&lt; %%&gt;)

  

变量被{{和}}

包围
<body>
    <h1>USN {{usn}}</h1>
    <h2>Name {{name}}</
    {% if department %}
        <h3>{{department}}</h3>
        <table>
            <ul>
            {% for r in result %}
                <li>{{r}}</li>
            {% endfor %}
            </ul>
         </table>
    {% endif %}
</body>

for循环中也有错误{% r in result %} 由于您正在考虑结果,因此您应该显示r:{{ r }}

相关问题