for循环报错

来源:3-3 模板语法之模板标签(下)

weixin_慕哥3021856

2023-06-19 18:09:58

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板标签</title>
    <style type="text/css">
        .odd {
            background-color: #ff0000;
            color: #fff;
        }
        .even {
            background-color: aqua;
            color: #000000;
        }
    </style>
</head>
<body>
    <h3>条件判断</h3>
    {% if var is none %}
    <p>var is none</p>
    {% else %}
    <p>var is not none</p>
    {% endif %}

    {% if a is defined %}
    <p>a已定义</p>
    {% else %}
    <p>a未定义</p>
    {% endif %}
    {% if a == 2 %}
    <p>a 等于 2</p>
    {% endif %}

    <h3>for循环dict -break</h3>
    {% for user in user_list -%}
        <p class="{{ loop.cycle('odd','even) }}">
            第{{ loop.index }}个用户,总共{{ loop.length }}个:<br/>
            {% for key, value in user.items() -%}
                {% if loop.index > 2 -%}
                    {% break -%}
                {% endif -%}
                {{ key }}: {{ value }}
            {% endfor -%}
        </p>
    {% else %}
        <p>用户信息为空</p>
    {% endfor %}

    <h3>for循环的使用</h3>
    {% for item in user_list -%}
        <p>用户名:{{ item.username }},年龄:{{ item.age }}</p>
    {% else %}
        <p>用户信息为空</p>
    {% endfor %}


    <h3>for循环 dict</h3>
    {% for user in user_list -%}
        <p class="{{ loop.cycle('odd','even') }}">
            第{{ loop.index }}个用户,总共{{ loop.length }}个:<br/>
            {% for key,value in user.items() %}
                {{ key }}:{{ value }}
            {% endfor -%}
        </p>
    {% else %}
        <p>用户信息为空</p>
    {% endfor %}

    <!--    添加注释-->
    <h3>赋值的使用</h3>
    {% with %}
        {set value = 42%}
        {{value}}只在当前代码块有用
    {% endwith %}
</body>
</html>

问题描述:

报错odd,而且style type被划掉,是不能用了吗?

相关截图:

https://img.mukewang.com/climg/649028f70973bac611450661.jpg

https://img.mukewang.com/climg/649028f709c93e8725601504.jpg

https://img.mukewang.com/climg/6490295f0992094f25601504.jpg

写回答

1回答

好帮手慕小猿

2023-06-19

同学,你好!横线表示type属性已弃用,应该和同学使用的工具版本有关。同学可以看下设置的css样式是否可以生效。

1、for循环是even 少了引号

https://img.mukewang.com/climg/6490332409b6bad409460418.jpg

2、后端传过来的变量是list_user,同学检查下自己后台传的数据是list_user还是user_list

https://img.mukewang.com/climg/649033940998592504690460.jpg

py文件

https://img.mukewang.com/climg/64903403094a703308940401.jpg

祝学习愉快~

0
heixin_慕哥3021856
hp>变量名都ok

h023-06-19
共1条回复

0 学习 · 4469 问题

查看课程

相似问题

回答 1

回答 1

回答 1

回答 1