zoukankan      html  css  js  c++  java
  • 模板语法

    模板语法

    views.py

    import datetime
    from django.shortcuts import render
    
    
    # Create your views here.
    
    
    def index(request):
        """
        模板语法由render方法渲染
    
        1. {{ var }}  渲染变量(所有数据类型)
            - 深度查询. 句点符: .
                <p>{{ fam.0 }}</p>
    
                <p>{{ person_lst.1.running }}</p>
                只能渲染无参方法
    
            - filter:过滤器
                {{ var|filter_name:参数}}
    
        2. {% %}  渲染标签(逻辑)
            - for 循环标签
    
            - if  判断标签
            -
        """
        # ################## 渲染变量 ##################
        name = 'sunny'
        age = 22
        fam_lst = ['sunny', 'vickey', 'ethan']
        info = {'name': 'sunny', 'age': 20, 'gender': 'male'}
    
        class Human(object):
            def __init__(self, cls_name, cls_age):
                self.name = cls_name
                self.age = cls_age
    
            def running(self):
                return '%s is running!' % self.name
    
        sunny = Human('sunny', 20)
        vickey = Human('vickey', 18)
        ethan = Human('ethan', 6)
    
        person_lst = [sunny, vickey, ethan]
    
        # ############## 过滤器 ##################
    
        # date: 日期 {{ now|date:"Y-m-d " }}
        now = datetime.datetime.now()
    
        # default: 默认值 {{ book_lst|default:"没有符合条件的数据" }}
        # book_lst = ['西游记', '三国演义', '红楼梦', '水浒传']
        book_lst = []  # 如果数据库取出的数据为空,可以显示default后的参数
    
        # length: 长度 {{ name|length}}
        name = 'sunny'
    
        # filesizeformat: 数字转bytes(Gb, Mb, Kb)  {{ filesize|filesizeformat }}
        filesize = 1024
    
        # slice: 切片  {{ content|slice:"2:-1" }}
        content = 'hello world'
    
        # truncatechars: 字符截断,不展示截断后的字符,由...代替  {{ article|truncatechars:4}}
        article = '山不在高有仙则名,水不在深有龙则灵'
        # truncatewords: 单词截断   {{ article|truncatewords:5}}
        article_eng = 'Today is a good day'
    
        # django模板渲染的时候如果遇到的是 标签字符串,则会转义成特殊字符返回浏览器,浏览器再解析为字符串形式
        #   - {{link}} 渲染成字符串, 防止脚本攻击,xss攻击
        #   - &lt;a href=&#39;https://www.baidu.com&#39;&gt;点我&lt;/a&gt;
    
        # safe: 表示此变量渲染的是安全的, 不必转义 {{link|safe}}
        #   - 如 评论的时候提交: <script>alert('11111')</script>, 如果不转义则会执行这段js代码
    
        link = "<a href='https://www.baidu.com'>点我</a>"  # 如果为safe则浏览器展示的是 链接标签
        tag = "<script>alert('11111')</script>"
    
        # ############## 标签渲染 ################
        four_book_lst = ['西游记', '三国演义', '红楼梦', '水浒传']
        # {% for %}
    
        # 循环计数器: {{ forloop.counter}}
        """
        {% for book  in four_book_lst %}
            <ul>
                <li>{{ forloop.counter }} {{ book }}</li>
            </ul>
        {% endfor %}
        """
        # {%if%}
    
        num = 101
        """
        { % if num > 100 %}
        < p > {{num}} 大于100 < / p >
        { % elif num == 100 %}
        < p > {{num}} 等于100 < / p >
        { % else %}
        < p > {{num}} 小于100 < / p >
         { % endif %}
        """
    
        # {% with %} 深度变量起别名
    
    
    
    # locals():包含所有变量
        return render(request, 'index.html', locals())
        # return render(request, 'index.html', {"name": name, "age": age, "fam": fam_lst, "fam_info": info})
    
    

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <h1>This is index page</h1>
    
    {# 变量 #}
    <h1>渲染各种数据类型</h1>
        
    {#传入的变量字符串中有空格 加双引号#}
    <p>Title: "{{ title }}" 年龄: {{ age }}</p>
        
    <p>姓名: {{ name }} 年龄: {{ age }}</p>
    <p>{{ fam_lst }}</p>
    <p>{{ fam_lst.0 }}</p>
    <p>{{ info.age }}</p>
    
    <p>{{ person_lst }}</p>
    <p>{{ person_lst.0.name }}</p>
    <p>{{ person_lst.1.running }}</p>
    
    <h1>过滤器</h1>
    <p>{{ now|date:"Y-m-d " }}</p>
    <p>{{ book_lst|default:"没有符合条件的数据" }}</p>
    <p>{{ name|length }}</p>
    <p>{{ filesize|filesizeformat }}</p>
    <p>{{ content|slice:"2:-1" }}</p>
    <p>{{ article|truncatechars:5 }}</p>
    <p>{{ article_eng|truncatewords:2 }}</p>
    <p>{{ link }}</p>
    <p>{{ link|safe }}</p>
    <p>{{ tag }}</p>
    <p>{{ tag|safe }}</p>
    
    {# 标签 #}
    <h1>标签渲染</h1>
    <h3>for循环展示数据</h3>
    循环计数器: {{ forloop.counter}}
    {% for book  in four_book_lst %}
        <ul>
            <li>{{ forloop.counter }} {{ book }}</li>
        </ul>
    
    {% endfor %}
    
    {% if num > 100 %}
        <p>{{ num }}大于100</p>
        {% elif num == 100 %}
        <p>{{ num }}等于100</p>
        {% else %}
        <p>{{ num }}小于100</p>
    {% endif %}
    
    {% for person in person_lst %}
        {% if person.age > 10 %}
            <p>{{ person.name }}  {{ person.age }}</p>
        {% endif %}
    {% endfor %}
    
    {% with person_lst.0.name as person1 %}
        <p>{{ person1 }}</p>
    {% endwith %}
    
    </body>
    </html>
    
  • 相关阅读:
    《算法竞赛入门经典》 例题35 生成元 (Digit Generator, ACM ICPC Seoul 2005,UVa)
    《算法竞赛入门经典》 例题35 生成元 (Digit Generator, ACM ICPC Seoul 2005,UVa)
    《算法竞赛入门经典》 例题35 生成元 (Digit Generator, ACM ICPC Seoul 2005,UVa)
    SVN分支
    SVN分支
    SVN 版本回退
    SVN 版本回退
    如何在excel中取消合并单元格后内容自动填充?
    如何在excel中取消合并单元格后内容自动填充?
    如何让自己像打王者荣耀一样发了疯、拼了命的学习?
  • 原文地址:https://www.cnblogs.com/relaxlee/p/12842665.html
Copyright © 2011-2022 走看看