【Day 20】 Django Template Tags 補充篇(1) – If & Logical Operators

接下來的幾天都會是對Template Tags的補充內容,有些東西可能前面有使用過,這邊可以當作再一次的複習。為避免看起來內容水分有點重,比較簡單的內容都會被塞到同一篇,沒直接跳過這些看起來簡單的內容是為了呼應系列文的初心兩個字,我希望能將Django的入門起點拉得越低越好。

測試環境

開始測試補充篇的各種原始碼之前,我們要先建好一個用來顯示執行結果的測試網頁。

(忘記怎麼啟動python虛擬環境或網頁伺服器可以看前面幾篇都有提到)

  1. members/templates資料夾下新增一個檔案testing.html,之後會把各種原始碼貼進去看結果。
  2. members/views.py新增view方法,用來處理訪問tetsing.html的請求。
    1
    2
    3
    4
    5
    ##上面都沒變所以省略...
    #下面是新增的內容
    def testing(request):
    template = loader.get_template('testing.html')
    return HttpResponse(template.render())
  3. members/urls.py註冊到testing.html的路徑,指定用剛建立的view方法來處理對該路徑的請求。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    from django.urls import path
    from . import views

    urlpatterns = [
    path('', views.index, name='index'),
    path('add/', views.add, name='add'),
    path('add/addrecord/', views.addrecord, name='addrecord'),
    path('delete/<int:id>', views.delete, name='delete'),
    path('update/<int:id>', views.update, name='update'),
    path('update/updaterecord/<int:id>', views.updaterecord, name='updaterecord'),
    path('testing/', views.testing , name='testing') #新增這一行
    ]
  4. 啟動Server等待查看每一次貼上原始碼的結果。
  5. 把各種原始碼貼進testing.html,就能在刷新頁面後,看到相應的結果囉。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    {% with greeting=2 %}
    {% if greeting == 1 %}
    <h1>Hello</h1>
    {% elif greeting == 2 %}
    <h1>Welcome</h1>
    {% else %}
    <h1>Goodbye</h1>
    {% endif %}
    {% endwith %}

下面的例子都改用{% with %}`建立變數來測試執行結果,別忘記加上`{% endwith %},W3Schools上面的沒有加,實際執行會有錯誤。


【條件判斷式】

if

if後方條件成立則執行if的區塊。

  • 條件: greeting是否等於1
    • 條件成立: 在瀏覽器會看到Hello
    • 條件不成立: 在瀏覽器會看不到任何東西
1
2
3
4
5
{% with greeting=1 %}
{% if greeting == 1 %}
<h1>greeting == 1</h1>
{% endif %}
{% endwith %}

(if條件成立)


elif

(至少有2個條件以上存在)

if後方條件成立則執行if的區塊,不成立則接續判斷elif的條件,若條件成立則執行elif的區塊。

1
2
3
4
5
6
7
{% with greeting=2 %}
{% if greeting == 1 %}
<h1>greeting == 1</h1>
{% elif greeting == 2 %}
<h1>greeting == 2</h1>
{% endif %}
{% endwith %}

(elif條件成立)


else

若沒有任何條件成立,則執行else的區塊。

1
2
3
4
5
6
7
8
9
{% with greeting=3 %}
{% if greeting == 1 %}
<h1>greeting == 1</h1>
{% elif greeting == 2 %}
<h1>greeting == 2</h1>
{% else %}
<h1>greeting == 3</h1>
{% endif %}
{% endwith %}

(ifelif條件均不成立)


【邏輯運算子】

若條件判斷不含邏輯運算子,則可判斷變數(variable)是否為空值(empty)。

1
2
3
4
5
{% with greeting=3 %}
{% if greeting %}
<h1>greeting is not empty</h1>
{% endif %}
{% endwith %}

(greeting不是空值,if條件成立)


==

可判斷變數(variable)是否與某數值相等。

1
2
3
4
5
{% with greeting=3 %}
{% if greeting == 3 %}
<h1>greeting == 3</h1>
{% endif %}
{% endwith %}

(greeting恰好等於3,if條件成立)


!=

可判斷變數(variable)是否不等於某數值。

1
2
3
4
5
{% with greeting=3 %}
{% if greeting != 4 %}
<h1>greeting != 4</h1>
{% endif %}
{% endwith %}

(greeting不等於4,if條件成立)


<

可判斷變數(variable)是否小於某數值。

1
2
3
4
5
{% with greeting=3 %}
{% if greeting < 4 %}
<h1>greeting < 4</h1>
{% endif %}
{% endwith %}

(greeting小於4,if條件成立)


<=

可判斷變數(variable)是否小於等於某數值。

1
2
3
4
5
{% with greeting=3 %}
{% if greeting <= 4 %}
<h1>greeting <= 4</h1>
{% endif %}
{% endwith %}

(greeting小於等於4,if條件成立)


>

可判斷變數(variable)是否大於某數值。

1
2
3
4
5
{% with greeting=5 %}
{% if greeting > 4 %}
<h1>greeting > 4</h1>
{% endif %}
{% endwith %}

(greeting大於4,if條件成立)


>=

可判斷變數(variable)是否大於等於某數值。

1
2
3
4
5
{% with greeting=5 %}
{% if greeting >= 4 %}
<h1>greeting >= 4</h1>
{% endif %}
{% endwith %}

(greeting大於等於4,if條件成立)


and

可用來同時比較2個條件,判斷條件1和條件2是否均成立。

1
2
3
4
5
{% with a=5 b=5 %}
{% if a == 5 and b == 5 %}
<h1>a == 5 and b == 5</h1>
{% endif %}
{% endwith %}

(a等於5、b等於5,if條件成立)


or

可用來同時比較2個條件,判斷條件1和條件2是否有至少1個成立。

1
2
3
4
5
{% with a=1 b=5 %}
{% if a == 5 or b == 5 %}
<h1>a == 5 or b == 5</h1>
{% endif %}
{% endwith %}

(a不等於5、b等於5,if條件成立)


and/or

同時使用andor的時候,要注意and前後的2個條件,實際上會被加上括號後解讀,下面的條件沒有加上括號,但相當於有加上括號的版本,if (a == 5 and b == 5) or a == 1,兩者是一樣的。

1
2
3
4
5
{% with a=1 b=5 %}
{% if a == 5 and b == 5 or a == 1 %}
<h1>a == 5 and b == 5 or a == 1</h1>
{% endif %}
{% endwith %}

(a等於1,if條件成立)


in

判斷是否有特定的item存在於object中。
下面例子判斷是否有特定的字串(String)存在於另一字串中。

String型別有自己的方法,它屬於object

1
2
3
4
5
{% with a='apple juice' %}
{% if 'apple' in a %}
<h1>'apple' in a</h1>
{% endif %}
{% endwith %}

('apple'存在於aif條件成立)


not in

判斷是否特定的item不存在於object中。
下面例子判斷是否特定的字串(String)不存在於另一字串中。

String型別有自己的方法,它屬於object

1
2
3
4
5
{% with a='apple juice' %}
{% if 'banana' not in a %}
<h1>'banana' not in a</h1>
{% endif %}
{% endwith %}

('banana'不存在於aif條件成立)


is

可用來判斷兩邊的變數是否同屬一個物件(object)。

為什麼要判斷是否屬於同一物件呢? 這就要說到pass by valuepass by reference,簡單來說,使用pass by reference會導致有可能同時修改到複數個指向同個記憶體位置的變數。(建立變數時,會指派1個記憶體位置負責存放它)

1
2
3
4
5
6
7
{% with a='c' b='c' %}
{% if a is b %}
<h1>a is b</h1>
{% else %}
<h1>a is not b</h1>
{% endif %}
{% endwith %}

在上面的例子中,ab的值都是'C',但它們兩個是分別被建立出來的object,所以它們不屬於同一物件,導致if的條件不成立、else的條件成立。


is not

可用來判斷兩邊的變數是否不屬於同一個物件(object)。

1
2
3
4
5
6
7
{% with a='c' b='c' %}
{% if a is not b %}
<h1>a is not b</h1>
{% else %}
<h1>a is b</h1>
{% endif %}
{% endwith %}

在上面的例子中,ab的值都是'C',但它們兩個是分別被建立出來的object,所以它們不屬於同一物件,導致if的條件成立、else的條件不成立。

isis not的判斷,比較像是在判斷運算子前後的兩個變數是否指向或不指向同個記憶體位置。

分享到