栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python3——模板标签

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python3——模板标签

第二课学习的是模板变量,这节课是当内容繁多时需要把模板进行归置


从文件角度,可以在大文件夹下做小的文件夹,把HTML文件和view视图文件进行打包

路由文件内容不会过多

 视图文件放如文件夹需要写init文件初始化


模板标签

if,for,foot(把HTML的body部分放在foot.html文件中),继承性child和parent

主要写在HTML文件body部分中

{% if a >= 90 %}
    优秀
    {% elif a >= 80 %}
    良好
    {% elif a >= 80 %}
    良好
    {% else %}
    不及格
{% endif %}
{% for student in students %}
    
  • {{ forloop.counter }} {{ student }} ,
  • {% empty %} 没有学生 {% endfor %} {% for score in scores %}
  • {{ forloop.counter }} {{ score }}
  • {% if not forloop.last %} ,{% endif %} {% endfor %}

      {% include 'foot.html' %}

    django中也可以做继承写,路由使用子HTML时,block里面使用子HTML的,外面继承父HTML的

    • title是子HTML的
    • 学生名字是子HTML的,是可以传参的
    • AI来自子HTML

    ”欢迎“和老师名字来自父HTML中没被继承的内容

     视图文件就是render写HTML,因为要传输参数,加locals()

    ————————————————————————父HTML
    
    
    
        
        {% block title %}
    
        {% endblock %}
    
    
    欢迎
    
    {% block content1 %}
    hello!!!!everybody!!!
    {% endblock %}
    
    {% block content2 %}
        jicheng
    {% endblock %}
    
    老师名字,{{ teachers.name }}
    
    
    
    ————————————————————————————子HTML
    ————————————————————————————不需要外面的标签
    {% extends "tab/parent.html" %}
    {% block title %}
        django
    {% endblock %}
    
    {% block content1 %}
        {% for student in students %}
            
  • {{ forloop.counter }} {{ student }}
  • {% endfor %} {% endblock %} {% block content2 %} AI {% endblock %}

    过滤器

    视图文件中写字符串作为参数,需要传参

    时间是现在的时间,所以再次刷新会发生变化

        s="helloworld你好"
        s1="byebyedjango"
        t=datetime.datetime.now()
        p=100
        s2='he say"byebyedjango"'
    
    
    
        
        模板过滤器
    
    
    传递字符的长度:{{ s |length }}
    传递字符的首字母:{{ s |first }}
    字母大小写转换:{{ s |upper }}
    字母小大写转换:{{ s |lower }}
    首字母大写:{{ s |capfirst }}
    首字母大写:{{ s |first |upper}}
    
    首字母大写:{{ s1 |capfirst }}
    首字母大写:{{ s1 |safe }}
    
    现在时间:{{ t|date }}
    现在时间:{{ t|date }}
    
    传递数字加法{{ p|add:10 }}
    
    

     

     

     

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/269868.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号