zoukankan      html  css  js  c++  java
  • Django学习路22_empty为空,forloop.counter 从1计数,.counter0 从0计数 .revcounter最后末尾数字是1,.revcounter0 倒序,末尾为 0

    当查找的数据不存在,返回为 空时
    在 html 中使用 {%empty%} 语句  进行显示
    def getstudents(request):
    
        students = Student.objects.all().filter(s_name = 'qwer')
        # 指定一个不存在的值,
        # students = Student.objects.all()
        stu_dict = {
        #     自己定义的字典
            'hobby':'play',
            'time':'5 years'
        }
    
        stu_data = {
            'students':students,
            'stu_dict':stu_dict
        #     stu_dict 是自己定义的字典
        }
    
        return render(request,'students.html',context=stu_data)

     

    html 中语句
    
        {% for stu in students %}
        <li> {{ stu.s_name }}</li>
            {% empty %}
            <h3>不存在该学生</h3>
        {% endfor %}
        <hr/><br/><br/>
    注:
    如果为 空,则输出 <h3> 不存在该学生</h3> 这条语句


    def getstudents(request):
    
        students = Student.objects.all()
        # 指定一个不存在的值,
        # students = Student.objects.all()
        stu_dict = {
        #     自己定义的字典
            'hobby':'play',
            'time':'5 years'
        }
    
        stu_data = {
            'students':students,
            'stu_dict':stu_dict
        #     stu_dict 是自己定义的字典
        }
    
        return render(request,'students.html',context=stu_data)
    forloop.counter 示例
    
        {% for stu in students %}
        <li> {{ forloop.counter }} : {{ stu.s_name }}</li>
        {% endfor %}
      

     


    forloop.counter0 示例
    
        {% for stu in students %}
        <li> {{ forloop.counter0 }} : {{ stu.s_name }}</li>
        {% endfor %}

     


    forloop.revcounter 示例
    
    
        {% for stu in students %}
        <li> {{ forloop.revcounter }} : {{ stu.s_name }}</li>
        {% endfor %}

     


    forloop.revcounter0 示例
    
    
        {% for stu in students %}
        <li> {{ forloop.revcounter0 }} : {{ stu.s_name }}</li>
        {% endfor %}

     


    2020-05-14

  • 相关阅读:
    leetcode刷题16
    leetcode刷题15
    leetcode刷题14
    leetcode刷题13
    UnityWebReqest和WWW,请求web数据打包到Android手机上,报错 Unknown error记录
    Unable to instantiate prefab. Prefab may be broken.(Unity2018.2.2报错)
    Unity 2018.4.0 回退到 2018.2.2 出现错误日志修复
    Windows系统中,使用Protobuf,编译出C#文件
    Unity长按Button,显示消息盒子
    Mac 端 查找UnityEngine.dll和UnityEngine.UI.dll
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12887676.html
Copyright © 2011-2022 走看看