码迷,mamicode.com
首页 > 其他好文 > 详细

Django学习路22_empty为空,forloop.counter 从1计数,.counter0 从0计数 .revcounter最后末尾数字是1,.revcounter0 倒序,末尾为 0

时间:2020-05-14 13:47:29      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:pre   指定   code   png   ext   image   tst   mic   nts   

当查找的数据不存在,返回为 空时
在 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

Django学习路22_empty为空,forloop.counter 从1计数,.counter0 从0计数 .revcounter最后末尾数字是1,.revcounter0 倒序,末尾为 0

标签:pre   指定   code   png   ext   image   tst   mic   nts   

原文地址:https://www.cnblogs.com/hany-postq473111315/p/12887676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!