django-模板中的for标签

django-for标签

for 标签内容

views.py

###test_for

def  test_if_for(request):
    dic={}
    dic['x']=20
    dic['lst']=['zs','ls','ww']

    return render(request,'test_if_for.html',dic)

test_if_for.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test_if_for</title>
</head>
<body>
    {% for name  in lst %}

        {% if forloop.first %} 第一个元素--- {%  endif %}
        <p>  {{forloop.counter}} {{name}}</p>
        {% if forloop.last %} 最后一个元素--- {%  endif %}
    {% empty %}
        当前无数据

    {% endfor %}
</body>
</html>

页面html

<html lang="en"><head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test_if_for</title>
</head>
<body><text>
    

         第一个元素--- 
        </text><p>  1 zs</p>
        
    

        
        <p>  2 ls</p>
        
    

        
        <p>  3 ww</p>
         最后一个元素--- 
    

</body></html>
posted @ 2021-08-02 14:08  yescarf  阅读(180)  评论(0编辑  收藏  举报