zoukankan      html  css  js  c++  java
  • (三)Django模板语言

    一、字典,列表,类在template模板中的使用

    在视图函数中,即views.py中进行传值操作,可通过render方法,进行传值

    from django.shortcuts import render
    
    def home(request):
        string = u"我在自学堂学习Django,用它来建网站"
        return render(request,"index.html",{"string":string})
    

    对应home.html模板应用

    使用{{ stirng }}(变量)语句, 表示取string变量

    <!DOCTYPE html>
    <html>
    <head>
    	<title></title>
    </head>
    <body>
    	<h1>{{ string }}</h1>
    </body>
    </html>
    

    注意string可以是字典、列表、字符串、数字等

    def test(request):
        string = "hello world"
        list_number = [1,2,4,5,6]
        dict_string = {"username":"jackyoung","age":15,"weight":"125斤"}
        return render(request,'base.html',{"string":string,"list_number":list_number,"dict_string":dict_string})
    

    对应home.html模板

    <!DOCTYPE html>
    <html>
    <head>
    	<title></title>
    </head>
    <body>
    	<h1>这是一个字符串:{{ string }}</h1>
    	<h1>这是一个列表:{{ list_number }} </h1>
    	<h1>这是一个字典:{{ dict_string }} </h1>
    	<ul>
    		<li>这是字典的值调用:{{ dict_string.username }}</li>
    		<li>这是字典的值调用:{{ dict_string.age }}</li>
    		<li>这是字典的值调用:{{ dict_string.weight }}</li>
    	</ul>
    </body>
    </html>
    

    二、循环语句使用

    def home(request):
        TutorialList = ["HTML","CSS","jQuery","Python","Django"]
        blank_list = []
        return render(request,'home.html',{"tutoriallist":TutorialList,"blank_list":blank_list})
    

    1、{% for ... %}(标签)语句,中for循环,for语法可以按照python语法来编写,使用后需要{% endfor %}结束标记

    {% for i in items %}{% endfor %}
    <p>for循环的使用</p>
    {% for i in tutoriallist %}
    {{ i }}<br>
    {% endfor %}
    

    2、{% empty %} 使用{% empty %}表示列表是空的会直接执行它下方的语句

    {% for j in blank_list %}
    <p>{{ j }}</p>
    {% empty %}
    <p>列表是空的!</p>
    {% endfor %}
    

    3、for语句的其他使用

    变量 描述
    forloop.counter 索引从 1 开始算
    forloop.counter0 索引从 0 开始算
    forloop.revcounter 索引从最大长度到1
    forloop.revcounter0 索引从最大长度到0
    forloop.first 当遍历的元素为第一项时为真
    forloop.last 当遍历的元素为最后一项时为真
    forloop.parentloop 用在嵌套的 for 循环中,获取上一层 for 循环的 forloop

    三、判断语句的使用

    {% if x is true %}{% endif %}
    {% if bool_value is True %}
    <p>为真</p>
    {% else %}
    <p>为假</p>
    {% endif %}
    

    四、继承父类模板

    {% extends "base.html" %} 语句,会把base.html的内容全部继承到新模板中,在模板中只需要填写该语句即可

    五、包含

    {% include "header.html" %}语句,会把header.html的内容包含到模板中,只需要该语句,就可以将header.html内容包含到新模板中

    六、块Block

    {% block content %}Content{% endblock %}语句,把新模板中的内容块,代替掉基础模板中内容块,记住:需要和{% extends "base.html" %}一起使用
    例如:
    1、基础模板中使用{% block content %}

    <!DOCTYPE html>
    <html>
    <head>
    	<title></title>
    </head>
    <body>
    {% block content %}
    
    {% endblock %}
    </body>
    </html>
    

    2、新模板中使用{% block content %}

    {% extends "123.html"%}
    {% block content %}
    <h1>这是4.html页面的内容</h1>
    {% endblock %}
    
  • 相关阅读:
    数据库表设计--备份记录的表设计优化
    WINDOWS CLUSTER -- 时间不同步导致的群集问题
    SQL Server--存在则更新问题
    MySQL--Ansible推送密钥实现免密码登录
    我命由我不由天
    hive中同源多重insert写法
    单文件数据库
    NTP时钟同步配置
    SQL中左连接on and条件和where条件执行先后顺序
    maven引入CDH依赖包
  • 原文地址:https://www.cnblogs.com/yangsun/p/11915545.html
Copyright © 2011-2022 走看看