zoukankan      html  css  js  c++  java
  • Django测试开发-9-templates模板标签

    一、Django模板标签之if,for,ifnotequal和ifequal

    1.if模板

    {% if condition1  %}
        ... display 1
    {% elif condition12 %}
        ... display 2
    {% else %}
     
      ... display 3
    {% endif %}
    
    ---------------------------------------------------
    {% if a == 10 %}
        <h1>a10 == {{ a }}</h1>
    {% elif a == 20 %}
        <h1>a20 == {{ a }}</h1>
    {% endif %}

    2.for模板

    {% for foo in lsit %}
    
        <li>foo == {{ foo }}</li>
    {% endfor %}
    ------------------------------------------------------
    {% for foo in c %}
    
        <h1>foo == {{ foo }}</h1>
    {% endfor %}

    3.ifnotequal和ifequal

    {% ifequal a b %}
        <h1>a与b相等</h1>
    {% else %}
        <h1>a与b不相等</h1>
    {% endifequal %}
    
    {% ifnotequal a b %}
        <h6>a与b不相等</h6>
    {% else %}
        <h6>a与b相等</h6>
    {% endifnotequal %}

    结果如图:

     二、Django模板标签之include,extends

    1.include标签

    include标签可以将html文件进行拆分,使得html文件能够复用。

    如一个网站,头部和底部一般都是公用的,只有中间部分是不同的,可以将公共的top和底部抽取出来,单独做成一个html,使用时,使用include标签进行引入即可。

     top.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <section>顶部导航</section>
        <h1>顶部内容</h1>
        <hr>
    
    </body>
    </html>

    应用:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Templates</title>
    </head>
    <body>
    {% include 'top.html'%}
    </body>
    </html>

    结果

    2.extends

    {% block content %} {% endblock content %}
    
    可以在母模板中添加多个块标签,每个块标签取不同的名称
     
    子模板可通过extends标签继承母模板,对于母模板中的block块,可以重写,也可以不重写

    继承

    {% extends "father.html" %}

    具体例子:

    father.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>父页面</title>
    </head>
    <body>
    <h1>博客园</h1>
    <h1>首页</h1>
    {% block changepage %}
        <p>我是变化之前的页面元素</p>
    {% endblock %}
    
    {% block notchangepage %}
        <p>子类不重写父类元素</p>
    {% endblock %}
    
    <h2>哈哈哈</h2>
    <h2>啦啦啦</h2>
    </body>
    </html>

    son.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>子页面</title>
    </head>
    <body>
    
    <h1>我是子页面</h1>
    {% extends "father.html" %}
    
    {% block changepage %}
    <p>我是变化之后的元素</p>
    {% endblock %}
    
    
    </body>
    </html>

    urls.py中添加方法:

    def extend(request):
    
        return render(request,"son.html")

    view.py

    url(r"^extend",views.extend)

    浏览器访问:http://127.0.0.1:8000/vote/extend

  • 相关阅读:
    Servlet的PrintWriter out = response.getWriter()使用
    Java Performance Optimization Tools and Techniques for Turbocharged Apps--reference
    Scrum介绍——续
    Scrum介绍
    CMM能力成熟度模型
    自定义Spark Partitioner提升es-hadoop Bulk效率——续
    $digest already in progress 解决办法——续
    $digest already in progress 解决办法
    linux tcpdump 抓包
    Asterisk——part 1
  • 原文地址:https://www.cnblogs.com/chushujin/p/12450355.html
Copyright © 2011-2022 走看看