1. 程式人生 > >六、模板語言:變數,過濾器,標籤等

六、模板語言:變數,過濾器,標籤等

模板語法之變數、模版之過濾器、模版之標籤、自定義標籤和過濾器

# 相當於print了該變數

urls.py

url(r'^index/', views.index),

views.py

from django.shortcuts import render

# Create your views here.
def index(request):
    name='s_jun'
    age=20
    li=[1,2,'s_jun','zone']
    l2=[]
    t1=(1,2,3,'a','b','3')
    s2={'h','t','d','q','b','v',9,6,4,3,}
    dic={'name':'s_jun','age':20,'str2':[9,'a',6,'x',3]}
    def test():
        print('test')
        return 'return test '
    print(test())
    # 類和對像
    class Person():
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def get_name(self):
            return self.name
        @classmethod
        def cls_test(cls):
            return 'cls'
        @staticmethod
        def static_test():
            return 'static'
        # 模板裡不支援帶引數
        # def get_name_cs(self,ttt):
        #     return self.name

    s_jun=Person('s_jun',20)
    zone=Person('zone',20)
    person_list=[s_jun,zone]
    person_dic={'s_jun':s_jun}
    # 函式不能列印記憶體地址,但可以把它放入到列表中,再列印記憶體地址(瞭解即可,(只打印記憶體地址))
    # locals 會把函式當中的變數傳遞給'index.html'的這個網頁中去。
    file_size=1024
    import datetime
    ctim=datetime.datetime.now()
    h2='<h2>我倒</h2>'
    script='<script>alert(1111111111)</script>'
    #  # return render( request,'index.html',{'name':name})
    user='s_jun'
    return render(request,'index.html',locals())


templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板語言之變數</title>
</head>
<body>
<p>名字:{{ name }}</p>
<p>年齡(數字型別):{{ age }}</p>
<p>列表:{{ li }}</p>
<p>元祖:{{ t1 }}</p>
<p>集合:{{ s2 }}</p>
<p>字典:{{ dic }}</p>
<p>函式:{{ test }}</p>
<p>對像:{{ s_jun }}</p>
<p>列表裡套物件:{{ person_list }}</p>
<p>字典裡套物件:{{ person_dic }}</p>
<hr>
<h3>深度查詢</h3>
<p>列表的第0個值:{{ li.0 }}</p>
<p>列表的第3個值:{{ li.3 }}</p>
<p>字典取值:{{ dic.name }}</p>
<p>字典取列表值:{{ dic.str2.2 }}</p>
<p>物件取資料屬性:{{ s_jun.name }}</p>
<p>物件取繫結給物件的函式屬性:{{ s_jun.get_name }}</p>
<p>物件取繫結給類的函式屬性:{{ s_jun.cls_test }}</p>
<p>物件取靜態方法:{{ s_jun.static_test }}</p>
<p>把物件列表中s_jun年齡取出來:{{ person_list.1.age }}</p>
{#不能調有引數的方法#}
<p>字串的方法:{{ name.upper }}</p>
<h3>模板語言之過濾器</h3>
{#後面就是個python中的函式,|前面的,是函式的第一個引數,冒號後面的是第二個引數#}
<p>統計字串長度:{{ name|length }}</p>
<p>統計列表長度:{{ li | length }}</p>
<p>過濾器之預設值:{{ l2| default:'空值' }}</p>
<p>過濾器之filesizeformat--1: {{443|filesizeformat }}</p>
<p>過濾器之filesizeformat--2: {{ file_size|filesizeformat }}</p>
<p>過濾器之不使用date:{{ ctim }}</p>
<p>過濾器之date:{{ ctim|date:'Y-m-d' }}</p>
{#前閉後開區間#}
<p>過濾器之slice:{{ li|slice:'2:-1' }}</p>
{#支援步長#}
{#<p>過濾器之slice-字串:{{ name | slice: '0:3:3'}}</p>#}
{#三個起步(顯示10個,加上3個...是13個)#}
<p>過濾器之truncatechars:{{ 'sdfaasdfdfsfpyyy'| truncatechars:13}}</p>
{#根據空格來 顯示的欄位#}
<p>過濾器之truncatewords:{{ '奪 jhk jh 我 kjhkl lkj 恥 囲 在 dslafj'| truncatewords:5 }}</p>
<p>過濾器之不用safe:{{ h2 }}</p>
<p>過濾器之用safe:{{ h2|safe }}</p>
<p>過濾器之不用safe:{{ script }}</p>
{#<p>過濾器之用safe:{{ script|safe }}</p>#}
<p>過濾器之用add:{{ 12|add:'1' }}</p>
<p>過濾器之用add:{{ 'egon'| add:'xxx' }}</p>
<hr>
<h3>模板語言之標籤</h3>
{% for bar in li %}
{{ forloop }}
    <p>{{ forloop.first }} --->{{ forloop.counter0 }} --->{{ forloop.revcounter }}--->{{ bar }} </p>
{% endfor %}
<hr>
{%  for bar in li %}
    {% for i in person_list %}
{#  取出外層是第幾次迴圈#}
 {{ forloop.parentloop.counter }}
<p> {{ forloop.first }}--->{{ forloop.counter0 }}---> {{ forloop.revcounter }}----> {{ bar }} </p>
{% endfor %}
{% endfor %}
<hr>
{# *************迴圈的物件是空,才會走到empty,而不是物件裡面的東西為空 #}
{% for bar in dic %}
<p>{{ bar }}</p>
{% empty %}
{% endfor %}
<hr>
{#只迴圈字典的話,取到的是key值#}
{% for bar in dic %}
<p> {{ bar }} </p>
{% endfor %}
<hr>
{#取到value的值#}
{% for bar in dic.values %}
    <p>{{ bar }}</p>
    {% empty %}
{% endfor %}
<hr>
{#取到key 和 value的值#}
{% for k,v in dic.items %}
<p>{{ k }}-----> {{ v }}</p>
{% empty %}
{% endfor %}
<hr>
{% if user %}
    <a href="">退出</a>
    {% else %}
    <a href="">登入</a>
    <a href="">註冊</a>
{% endif %}
<hr>
{#for迴圈判斷如果是第一次,列印第一次,其他列印正常值#}
{% for bar in li %}
    {% if forloop.first %}
        <p>第一次的我</p>
    {% elif forloop.last %}
        <p>最後的我</p>
    {% else %}
        <p>{{ bar }}</p>
    {% endif %}
{% endfor %}
<hr>
{#with 相當於取別名,作用:變數太長,可以簡化#}
{% with name as ttt %}
    {{ ttt }}
    {{ name }}
    {{ user }}
{% endwith %}
<hr>
{% with dic.li.2 as ttt %}
    {{ ttt }}
    {{ ttt }}
    {% endwith %}
<hr>
{#************for ,if,with 都要有結束*************#}
<h3>自定義標籤過濾器</h3>
{% load mytag %}
{#傳多個引數的話:可以:'aaa:bb:cc',也可以:傳列表#}
<p>{{ 's_jun'| yyy:'nb' }}</p>
<h5>使用自定義的標籤</h5>
<p>{% add_nb 's_jun' %}</p>
<p>{% add_3 's_jun' 'is' 'zone' %}</p>
<hr>
{#過濾器,可以用在if判斷中#}
{% if 's_jun'|yyy:'test' %}
<p></p>
{% endif %}
{#標籤不能用在if判斷(報錯)#}
{#{% if add_nb 's_jun' %}#}
{##}
{#{% endif %}#}
</body>
</html>