Django-06 模板層變量 if標籤、for標籤 模板標籤 1, if標籤 2,for標籤

能傳遞到模板中的數據類型

  • str - 字符串
  • int - 整型
  • list - 數組
  • tuple - 元組
  • dict - 字典
  • func - 方法
  • obj - 類實例化的對象

在模板中使用變量語法

  • {{ 變量名 }}
  • {{ 變量名.index }}
  • {{ 變量名.key }}
  • {{ 對象.方法 }}
  • {{ 函數名 }}

例子

views.py

def test_html(request):
    from django.shortcuts import render
    dic = {}
    dic['int'] = 88
    dic['str'] = 'wanger'
    dic['lst'] = ['Tom','Jack','Lily']
    dic['func'] = say_hi
    dic['class_obj'] = Dog()
    return render(request,'test_html.html',dic)

def say_hi():
    return 'hahaha'

class Dog:
    def say(self):
        return 'wangwang'

test_html.html

<body>
    <h3>int 是 {{ int }}</h3>
    <h3>str 是 {{ str }}</h3>
    <h3>lst 是 {{ lst }}</h3>
    <h3>lst.0 是 {{ lst.0 }}</h3>
    <h3>dict 是 {{ dict }}</h3>
    <h3>dict['a'] 是 {{ dic.a }}</h3>
    <h3>function 是 {{ func }}</h3>
    <h3>class_obj 是 {{ class_obj.say }}</h3>
</body>

模板標籤

作用:將一些服務器端的功能嵌入到模板中,例如流程控制等
標籤語法

{% 標籤 %}
...
{% 結束標籤 %}

1, if標籤

{% if 條件表達式1 %}
...
{% elif 條件表達式2 %}
...
{% elif 條件表達式3 %}
...
{% else %}
...
{% endif %}

注意:
1,if條件表達式裏可以用的運算符==、!=、<、>、>=、in、not in 、is、is not、and、or
2,在if標記中使用實際括號是無效的語法。如果您需要它們指示優先級,則應使用嵌套的if標記。

djanggo中 if 的使用必須是:

{%if ♦ 變量名 ♦ 判斷符 ♦ 條件%}

其中♦ 表示空格,否則會報錯:

Could not parse the remainder: ‘%‘ from ‘%‘ 以及 Could not parse the remainder: ‘==“addd“‘ from ‘==“ad

小練習:計算器

mycal.html

<form action="/mycal" method="post">
        <input type="text" name="x" value="{{ x }}">
        <select name="op">
            <option value="add" {% if op == 'add' %} selected{%endif%}>+</option>
            <option value="sub" {% if op == 'sub' %} selected{%endif%}>-</option>
            <option value="mul" {% if op == 'mul' %} selected{%endif%}>*</option>
            <option value="div" {% if op == 'div' %} selected{%endif%}>/</option>
        </select>
        <input type="text" name="y" value="{{ y }}">=
        <span>{{ result }}</span>
        <div><input type="submit" value='開始計算'></div>
    </form>

views.py

def test_mycal(request):
    if request.method == 'GET':
        return render(request,'mycal.html')
    elif request.method == 'POST':
        x = int(request.POST['x'])
        y = int(request.POST['y'])
        op = request.POST['op']
        result = 0
        if op == 'add':
            result = x + y
        elif op == 'sub':
            result = x - y
        elif op == 'mul':
            result = x * y
        elif op == 'div':
            result = x / y
        return render(request,'mycal.html',locals())

2,for標籤

語法

{% for 變量 in 可迭代對象 %}
  ...循環語句
{% empty %}
  ... 可迭代對象無數據時填充的語句
{% endfor %}

內置變量-forloop

變量 描述
forloop.counter 循環的當前迭代(從1開始索引)
forloop.counter() 循環的當前迭代(從0開始索引)
forloop.revcounter counter值的倒序
forloop.revcounter() counter()值的倒序
forloop.first 如果這是第一次通過循環,則爲真
forloop.last 如果這是最後一次循環,則爲真
forloop.parentloop 當嵌套循環,parentloop表示外層循環
    {% for name in lst %}
        {% if forloop.first %}$$${%endif%}
        <p>{{ forloop.counter }} {{name}} </p>
        {% if forloop.last %}---{%endif%}
    {% empty %}
    當前沒數據
    {% endfor %}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章