zoukankan      html  css  js  c++  java
  • Tags 母板 继承母板 组件 静态文件

    Tags

    for

    <ul>
    {% for user in user_list %}
        <li>{{ user.name }}</li>
    {% endfor %}
    </ul>

    for循环可用的一些参数:

    VariableDescription
    forloop.counter 当前循环的索引值(从1开始)
    forloop.counter0 当前循环的索引值(从0开始)
    forloop.revcounter 当前循环的倒序索引值(从1开始)
    forloop.revcounter0 当前循环的倒序索引值(从0开始)
    forloop.first 当前循环是不是第一次循环(布尔值)
    forloop.last 当前循环是不是最后一次循环(布尔值)
    forloop.parentloop 本层循环的外层循环

    for ... empty

    复制代码
    <ul>
    {% for user in user_list %}
        <li>{{ user.name }}</li>
    {% empty %}
        <li>空空如也</li>
    {% endfor %}
    </ul>
    复制代码

    if,elif和else

    复制代码
    {% if user_list %}
      用户人数:{{ user_list|length }}
    {% elif black_list %}
      黑名单数:{{ black_list|length }}
    {% else %}
      没有用户
    {% endif %}
    复制代码

    当然也可以只有if和else

    {% if user_list|length > 5 %}
      七座豪华SUV
    {% else %}
        黄包车
    {% endif %}

    if语句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判断。

    with

    定义一个中间变量

    {% with total=business.employees.count %}
        {{ total }} employee{{ total|pluralize }}
    {% endwith %}

    csrf_token

    这个标签用于跨站请求伪造保护。

    在页面的form表单里面写上{% csrf_token %}

    注释

    {# ... #}

    注意事项

    1. Django的模板语言不支持连续判断,即不支持以下写法:

    {% if a > b > c %}
    ...
    {% endif %}

    2. Django的模板语言中属性的优先级大于方法

    def xx(request):
        d = {"a": 1, "b": 2, "c": 3, "items": "100"}
        return render(request, "xx.html", {"data": d})

    如上,我们在使用render方法渲染一个页面的时候,传的字典d有一个key是items并且还有默认的 d.items() 方法,此时在模板语言中:

    {{ data.items }}

    默认会取d的items key的值。

    母板

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Title</title>
      {% block page-css %}
      
      {% endblock %}
    </head>
    <body>
    
    <h1>这是母板的标题</h1>
    
    {% block page-main %}
    
    {% endblock %}
    <h1>母板底部内容</h1>
    {% block page-js %}
    
    {% endblock %}
    </body>
    </html>

    注意:我们通常会在母板中定义页面专用的CSS块和JS块,方便子页面替换。

    继承母板

    在子页面中在页面最上方使用下面的语法来继承母板。

    {% extends 'layouts.html' %}

    块(block)

    通过在母板中使用{% block  xxx %}来定义"块"。

    在子页面中通过定义母板中的block名来对应替换母板中相应的内容。

    {% block page-main %}
      <p>世情薄</p>
      <p>人情恶</p>
      <p>雨送黄昏花易落</p>
    {% endblock %}

    组件

    可以将常用的页面内容如导航条,页尾信息等组件保存在单独的文件中,然后在需要使用的地方按如下语法导入即可。

    {% include 'navbar.html' %}

    静态文件相关

    {% load static %}
    <img src="{% static "images/hi.jpg" %}" alt="Hi!" />

    引用JS文件时使用:

    {% load static %}
    <script src="{% static "mytest.js" %}"></script>

    某个文件多处被用到可以存为一个变量

    {% load static %}
    {% static "images/hi.jpg" as myphoto %}
    <img src="{{ myphoto }}"></img>

    使用get_static_prefix

    {% load static %}
    <img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!" />

    或者

    {% load static %}
    {% get_static_prefix as STATIC_PREFIX %}
    
    <img src="{{ STATIC_PREFIX }}images/hi.jpg" alt="Hi!" />
    <img src="{{ STATIC_PREFIX }}images/hi2.jpg" alt="Hello!" />

    自定义simpletag

    和自定义filter类似,只不过接收更灵活的参数。

    定义注册simple tag

    @register.simple_tag(name="plus")
    def plus(a, b, c):
        return "{} + {} + {}".format(a, b, c)

    使用自定义simple tag

    {% load app01_demo %}
    
    {# simple tag #}
    {% plus "1" "2" "abc" %}

    inclusion_tag

    多用于返回html代码片段

    示例:

    templatetags/my_inclusion.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}
    复制代码

    templates/result.html

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

    templates/index.html

    复制代码
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="x-ua-compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>inclusion_tag test</title>
    </head>
    <body>
    
    {% load my_inclusion %}
    
    {% show_results 10 %}
    </body>
    </html>


    总结:
    1. 母版
                就是一个普通的HTML文件,提取多个页面的公共部分
                减少代码量  修改十分方便
                
                定义block块 
                
            2. 使用(继承)
                在子页面中
                    {% entends 'base.html' %}
                    重新修改block块中的内容
            3. 注意事项:
                1. 把{% extends 'base.html' %} 写在第一行
                2. 修改的内容写在block中,写在外面不显示
                3. {% extends name %}   name应该是变量 或者是字符串  继承母版的名字
                4. 母版中定义多个block块,一般还有 js 块 和 css块
                    
        
        3. 组件
            1. 就是一小段HTML代码   多个页面都用到的内容 nav.html
            2. 在任意的模板中使用
                {% include 'nav.html' %}
            
                
            
        4. 静态文件相关
        
            1. {% load staticfiles %} 或者 {% load static %}
            2. {% static 'css/pub.css' %}  ——》 先去settings中获取STATIC_URL的配置,和后面提供的参数进行拼接
            
            1. {% load static %}
            2. 
                <link rel="stylesheet" href="{% get_static_prefix %}css/pub.css">
            
            
            
        5. 自定义simpletag和自定义inclusion_tag
            1. 在app下创建一个名叫templatetags的python包
            2. 在templatetags里建一个py文件
            3. 在py文件中编辑:
                from django import template
                register = template.Library()
                
                
                @register.simple_tag
                def join_str(arg1, arg2, arg3,*args,**kwargs):
                    print(args)
                    print(kwargs)
                    return '_'.join([arg1, arg2, arg3])+'*'.join(args)
                    
                @register.inclusion_tag('pagination.html')
                def pagination(total, current):
                    return {'total': range(1, total + 1), 'current': current}
    
            4.使用
                {% load py文件名 %}
                {% 函数名 参数1 参数2 %}
                
     自己总结:
    母板继承:
                      1,建立母板base.html(写入需要继承的代码)    
     2,在子页面.html中引入  {% entends 'base.html' %}
    3,在base.html中定义块   {% block page-mian %}
    内容1
         {% endblock %} 
    4,在子页面.html中导入:  {% block page-mian %}      #补丁替代,替代内容1的内容
    内容2
         {% endblock %} 
    写内容2将覆盖内容1,不写内容2,页面空
      组件:
    1, 创建nav,html (写需要添加的功能的代码)
    2, 在需要添加的 base.html页面中.需要添加的地方引入   {% include 'nav.html' %}     
    
    静态文件  :         
    在放有static的标签上面导入. {% load staticfiles %} 或者 {% load static %}
          1,   引入   {% load staticfiles %} 
     把  <link rel="stylesheet" href="/static/css/pub.css/">    改成 
    <link rel="stylesheet" href="{% static 'css/pub.css' %}">
    2,或者引入  {% load static %}
    把  <link rel="stylesheet" href="/static/css/pub.css/">    改成 
    <link rel="stylesheet" href="{% get_static_prefix %}css/pub.css">
    
     自定义simpletag和自定义inclusion_tag
    simpletag:
    1. 在app下创建一个名叫templatetags的python包
            2. 在templatetags里建一个py文件(myfilters.py)
            3. 在py(myfilters.py) 文件中编辑:
                from django import template
                register = template.Library()
                
                
                @register.simple_tag
                def join_str(arg1, arg2, arg3,*args,**kwargs):
                    print(args)
                    print(kwargs)
                    return '_'.join([arg1, arg2, arg3])+'*'.join(args)
                    
                @register.inclusion_tag('pagination.html')
                def pagination(total, current):
                    return {'total': range(1, total + 1), 'current': current}
    
            4.使用在 template_test.html中导入:
                {% load py文件名 %}      #{% load myfiters %}
                {% 函数名 参数1 参数2 %}     #{% join_str 'xiaofeng'  'duanyu'  'xuzhu'%}
    
    inclusion_tag:
    1. 在app下创建一个名叫templatetags的python包
            2. 在templatetags里建一个py文件(mytags.py)
            3. 在py(mytags.py) 文件中编辑:
    from django import template
    register = template.Library()
    @register.inclusion_tag('pagination.html')
    def pagination(total, current):
                    return {'total': range(1, total + 1), 'current': current}
    4,创建分页器:pagination.html
    <div class="text-center">
                <nav aria-label="...">
                <ul class="pagination">
                    <li class="disabled"><a href="#" aria-label="Previous"><span
                        aria-hidden="true">«</span></a>
                    </li>
                    {% for num in total %}
                    {% if num == current %}
                        <li class="active"><a href="#">{{ num }}</a></li>
                    {% else %}
                        <li><a href="#">{{ num }}</a></li>
                        {% endif %}
    
                    {% endfor %}
                    <li><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>
                </ul>
        </nav>
    </div>
    
    5,在base.html中导入:
    {% load mytags %}
    {% pagination 10 5%}  #一页显示10个 ,显示在5上
    流程:
    mytags.py写函数  ----->   pagination.html 渲染 ------> base.html导入
    View Code


  • 相关阅读:
    python调用函数
    python递归函数的执行过程
    linux rwx 权限说明
    linux ssh scp免密码
    linux的bash特性
    python3常用的内置函数
    linux清理系统缓存
    vim常用命令
    公司项目安装禅道
    jquery 自定义动画
  • 原文地址:https://www.cnblogs.com/ls13691357174/p/9625660.html
Copyright © 2011-2022 走看看