zoukankan      html  css  js  c++  java
  • Python

    simple_tag:

    simple_tag 和自定义 filter 类似,但可以接收更多更灵活的参数

    在 app01/templatetags/ 目录下创建 mysimple_tag.py

    mysimple_tag.py:

    from django import template
    register = template.Library()
    
    
    @register.simple_tag(name="cal")
    def cal(arg1, arg2, arg3, arg4):
        return "{}+{}+{}+{}".format(arg1, arg2, arg3, arg4)
    

    test.html:

    {% load mysimple_tag %}
    
    {% cal "abc" "def" "ghi" "jkl" %}
    

    运行结果:

    inclusion_tag:

    多用于返回 html 代码

    在 app01/templatetags/ 目录下创建 myinclusion_tag.py

    myinclusion_tag.py:

    from django import template
    register = template.Library()
    
    
    @register.inclusion_tag("result.html")
    def show_results(n):
        n = 1 if n < 1 else int(n)
        data = ["第{}项".format(i) for i in range(1, n+1)]
        return {"data": data}
    

    result.html:

    <ul>
      {% for choice in data %}
        <li>{{ choice }}</li>
      {% endfor %}
    </ul>
    

    test.html:

    {% load myinclusion_tag %}
    
    {% show_results 10 %}
    

    运行结果:

  • 相关阅读:
    统计学基础
    ip地址分类
    OSI七层协议与TCP/IP模型、三次握手与四次挥手
    计算机编码
    [HNOI2008]Cards
    P4309 [TJOI2013]最长上升子序列
    P3794 签到题IV
    P2605 [ZJOI2010]基站选址
    UVA10791
    P3825 [NOI2017]游戏
  • 原文地址:https://www.cnblogs.com/sch01ar/p/11266420.html
Copyright © 2011-2022 走看看