zoukankan      html  css  js  c++  java
  • Django学习手册

    相关:

    Django的模板引擎提供一般性的功能函数,通过前端可以实现多数的代码逻辑功能,但它仅支持大多数常见情况下的函数功能,例如if判断,ifequal对比返回值等,复杂一些的函数功能并不支持,例如通过模板来判断一个返回值是否是合法的数字类型,如果又不希望通过后台视图代码来实现的话,就可以自定义一些前端函数功能。

    Django提供了两种方式

        simple_tag

        filter

    两种方式前置操作:

    1.app下创建 templatetags 目录

    2.在目录下创建py文件。

     

    3.在文件内 创建template对象 register

    from django import template
    
    register = template.Library()

    4.setting 中注册app

     

    5.定义函数

      simple_tag 方式:

      模板导入符为 {%  函数 参数1  参数2 参数**  %}

      优点:可传递多个参数。    缺点:不能作为if条件。

    from django import template
    
    register = template.Library()
    
    @register.simple_tag
    def simple_type(num): 

      return num ** 2

      filter 方式:

      模板导入符为 {{  参数1| 函数名:参数2  }}

      优点:可作为if条件。  缺点:最多两个参数,导入符终函数名后不能有空格

    from django import template
    
    register = template.Library()
    
    @register.filter
    def filter_type(a1,a2):
        return a1 + a2

    6.在模板中加载,并应用

    {% load seting %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <div>
            {% simple_type 8 %}
    
            {{ "AAAAA"|filter_type:"BBBBB" }}
        </div>
    </body>
    </html>
  • 相关阅读:
    优先队列
    Problem W UVA 662 二十三 Fast Food
    UVA 607 二十二 Scheduling Lectures
    UVA 590 二十一 Always on the run
    UVA 442 二十 Matrix Chain Multiplication
    UVA 437 十九 The Tower of Babylon
    UVA 10254 十八 The Priest Mathematician
    UVA 10453 十七 Make Palindrome
    UVA 10163 十六 Storage Keepers
    UVA 1252 十五 Twenty Questions
  • 原文地址:https://www.cnblogs.com/Anec/p/9566396.html
Copyright © 2011-2022 走看看