Django常用模板语言

Django模板语言常用

  • 前言:Web框架的模式

    • 1.MVC框架
    • 2.MTV框架
    • 5XwjSu.png
  • Django的模板语言

    • 变量相关: {{ }}
    • 逻辑相关的使用 :
  • 变量

    • 在Django的模板语言中语言使用: {{ 变量名}}

    • 点(.) 在模板语言中同样是万能的

    • {# 取l中的第一个参数 #}
      {{ l.0 }}
      {# 取字典中key的值 #}
      {{ d.name }}
      {# 取对象的name属性 #}
      {{ person_list.0.name }}
      {# .操作只能调用不带参数的方法 #}
      {{ person_list.0.dream }}
      
  • 模板语言之标签 {% tag %}

    • for标签 循环的序号可以通过{{forloop}}显示

      • <h3>循环取值1</h3><hr>
        {% for item in person_list %}
            <p>{{ item.name }},{{ item.age }}</p>
        {% endfor %}
        
        <h3>循环取值2:倒序</h3><hr>
        {% for item in person_list reversed %}
            <!--序号从1开始-->
            <p>{{ forloop.counter }}----->{{ item.name }},{{ item.age }}</p>
            <!--序号从0开始--><p>{{ forloop.counter0 }}----->{{ item.name }},{{ item.age }}</p><!-- 序号倒序 --><p>{{ forloop.revcounter }}----->{{ item.name }},{{ item.age }}</p>
        {% endfor %}
        
        <h3>循环取值3:字典</h3><hr>
        {% for k,v in d.items %}
            <p>{{ k }},{{ v}}</p>
        {% endfor %}
        
      • for循环中的一些可用参数

      • Variable Description
        forloop.counter 当前循环的索引值(从1开始)
        forloop.counter0 当前循环的索引值(从0开始)
        forloop.revcounter 当前循环的倒序索引值(从1开始)
        forloop.revcounter0 当前循环的倒序索引值(从0开始)
        forloop.first 当前循环是不是第一次循环(布尔值)
        forloop.last 当前循环是不是最后一次循环(布尔值)
        forloop.parentloop 本层循环的外层循环
    • for...empty:for 标签带有一个可选的{% empty %}从句,以便在给出的组为空或者没有找到的时,添加操作

      • {% for person in person_list %}
            <p>{{ person.name }}</p>
        
        {% empty %}
            <p>sorry,no person here</p>
        {% endfor %}
        
  • if标签 {% if 条件 %} {{ 执行语句 }} {% endif %}

    • if语句支持and,or,==,<,>, <=, >=, in , not in, is, is not判断

    • {% if i > 300 %}
          <p>大于{{ i }}</p>
      {% elif i == 200  %}
          <p>等于{{ i }}</p>
      {% else %}
          <p>小于{{ i }}</p>
      {% endif %}
      
  • with 语句 定义一个中间变量

    • with 上下文管理器协议

    • 应用场景:网络连接,数据库连接,文件句柄,用到锁

    • {% with total=business.employees.count %}
          {{ total }} employee{{ total|pluralize }}
      {% endwith %}
      
      {% with file_size as fs %}
           <h1>{{ fs }}</h1>
      {% endwith %}
      
  • csrf_token 这个标签用于跨站请求伪造保护

    • 在页面的form表单下一行写上
    • 这样就可以不将setting.py中的46行的csrf..给注释掉了
  • Filters(过滤器)

    • 在模板语言中,通过使用过滤器来改变变量的显示
    • 语法: {{ value|filter_name:参数}}
      • |之后为过滤器
      • 过滤器支持链式操作,
      • 过滤器支持参数
      • 过滤器左右没有空格没有空格没有空格
  • 1, default: 如果一个变量是false或者为空,使用给定的默认值。否则,使用变量值

    • <p>default过滤器:{{ li|default:"如果显示为空,设置的解释性的内容" }}</p>
      
  • 2, length:返回值的长度,对字符串和列表,字典都起作用

    • <h1>{{ list1|length }}</h1>
      <h1>{{ dic|length }}</h1>
      #如果 list1 是 ['a', 'b', 'c', 'd'],那么输出是 4
      
  • 3, filesizeformat: 将值格式化为一个“人类可读的”文件尺寸

    • <h1>{{ value|filesizeformat }}</h1>
      #如果value是12345678 ,输出将会是117.7MB
      
  • 4, date: 如果 value= datetime.now()

    • <h1>{{ time|date:"Y-m-d H:i:s" }}</h1>
      #导入模块 from datetime import datetime
      # time = datetime.now()
      #输出为 当前时间  2018-11-01 17:57:45
      
  • 5, slice: 切片

    • <h1>{{ num|slice:"0:5" }}</h1>
      #num = "hello world”  输出为hello,同样遵循顾头不顾尾
      
  • 6,truncatechars 截断

    • 如果字符串子多于指定的字符数量,那么会被截断,截断的字符串将以可翻译的省略号序列(...)结尾

    • <h1>截断字符{{ num|truncatechars:5 }}</h1>      #num = "hello world" 输出为  he...
      <h1>截断单词{{ num|truncatewords:5 }}</h1>      #单词是以空格进行区分,num="h e l l o w o r l d" 输出为 h e l l o ...
      
  • 7, safe 为了保证html在某些情况下不被转义,就使用safe。

    • <h1>{{ a }}</h1>   # a = "<a href='https://www.baidu.com'>百度</a>" 输出为字符串
      <h1>{{ a|safe }}</h1>   #当链接是安全的,则是一个链接标签
      
  • 8,cut 移除value中所有与给出的变量相同的字符串

    • <h1>{{ num|cut:"l" }}</h1>  #num = "hello world"  输出为  heo word
      
  • 9, join 使用字符串连接列表,与 Python中的 str.join(list)相同

    • <h1>{{ list3|join:"-" }}</h1>   #list3 = ["h", "e", 'l', 'l', 'o']   输出为 h-e-l-l-o
      
  • 10, timesince 将日期格式设为日期起的时间,采用一个可选参数,

    • <h1>{{ hours|timesince }}</h1>  # hours = datetime.now() - timedelta(hours=4)  输出为 4 hours
      
  • 自定义filter

    • 1, 在app下面新建一个Python package名字必须叫做 templatetags
    • 2, 在templatetags中新建一个py文件
    • 3, 在py文件中写自定义的filter函数
    • 4, 告诉django我现在多了一个自定义的filter
      • 5XwkIQ.png
  • 5, 使用自定义的filter

    • {% load new_filter %}
      <p>{{ name|new_filter }}</p>   #name = "alex"   输出为  alexsb
      
    • 在html中导入刚才创建的文件

    • 重启Django项目

    • 按照普通filter调用自定义的函数

    • 5m339O.png

  • 母版的定义和使用

    • 什么情况下使用母版

      • 当多个页面的大部分内容都一样的时候,我们可以把相同的部分提取出来,放到一个单独的母版中
    • 使用

      • 在母版中定义需要被替换的block

        • {% block page-css %}
          {% endblock %}
          
          {% block page-main %}
          {% endblock %}
          
          
          {% block page-js %}
          {% endblock %}
          
      • 在子页中

        • 先继承母版

          • {% extends “母版的html" %}
            {% block page-main %}
                正文
            {% endblock %}
            
    • 组件

      • 当页面上相对独立的某个部分可以单独拿出来放到一个单独的html文件中

      • 语法

        • {% inclue '组件.html' %}
          
    • 静态文件相关

      • 将静态文件的路劲改成动态拼接,避免在html页面中应编码静态文件导入路劲

      • 俩种用法

      • {% load static %}
        # 第一种 常用
        <script src="{% static 'bootstrap-3.3.7/js/bootstrap.js' %}"></script>
        # 第二种
        <link href="{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css">
        
    • 自定义simpletag

      • 步骤类似于自定义filter
      • 5mFE5n.png
      • 5mF1Qa.png
    • 自定义的inclusion_tag

      • 1.接收数据返回一段HTML代码(类似与render函数)
posted @ 2018-11-06 15:45  云丛  阅读(211)  评论(0编辑  收藏  举报