2024年2月12日 星期一

Django 5.0: 在模版上(Template)使用filter過濾器

 參考資料:Python架站特訓班Django 3最強實戰

延續專案:用擲骰子來說視圖(View)如何傳送變數到模版(Template)

1. 設定filter的URL,passdata\urls.py。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
URL configuration for passdata project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from passdataapp.views import dice, dice2, dice3, show, filter

urlpatterns = [
    path('admin/', admin.site.urls),
    path('dice/', dice),
    path('dice2/', dice2),
    path('dice3/', dice3),
    path('show/', show),
    path('filter/', filter),
]

2.定義filter()函式passdataapp\views.py。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django.shortcuts import render
import random #加入random模組

# Create your views here.
def dice(request):
    no = random.randint(1, 6) #亂數產生1-6其中一個數字
    return render(request, "dice.html", {"no":no})

def dice2(request):
    no1 = random.randint(1, 6) #亂數產生1-6其中一個數字
    no2 = random.randint(1, 6) #亂數產生1-6其中一個數字
    no3 = random.randint(1, 6) #亂數產生1-6其中一個數字
    #使用locals()函式來傳遞所有的區域變數
    return render(request, "dice2.html", locals())
    
times = 0
def dice3(request):
    global times    #宣告global變數
    times = times + 1
    local_times = times
    username = "David"
    no = random.randint(1, 6) #亂數產生1-6其中一個數字
    return render(request, "dice3.html", locals())

def show(request):
	student1 = {"name":"王老五","phone":"0928-123456", "age":20}
	student2 = {"name":"林大智","phone":"0928-123457", "age":21}
	student3 = {"name":"陳三省","phone":"0928-123458", "age":22}
	student4 = {"name":"董小于","phone":"0928-123459", "age":20}
	student5 = {"name":"曾小平","phone":"0928-123450", "age":19}
	students = [student1, student2, student3, student4, student5]
	return render(request, "show.html", locals())
	
def filter(request):
	value = 100
	list1 = [1,2,3,4,5]
	password = "芝麻開門"
	html= "<h1>哈囉</h1>"
	value2 = False
	value3 = "a b c"
	return render(request, "filter.html", locals())

3.新增templates\filter.html檔案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<head>
	<meta charset='utf-8'>
	<title>過濾器</title>
</head>
<body>
	<h6>
		value={{value}}, vlaue|add:"5"={{ value|add:"2" }} <br/>
		value|stringformat:"E"={{ value|stringformat:"E" }} <br/>
		list1= {{ list1 }}, list1|length = {{ list1|length }}, list1|slice:"2:" = {{ list1|slice:"2:" }} <br/>
		{% if password == "芝麻開門" %}
			密碼正確 <br/>
		{% else %}
			密碼錯誤 <br/>
		{% endif %}
			value2 = {{ value2 }}, value2|yesno:" 是, 否, 取消"={{ value2|yesno:" 是, 否, 取消" }} <br/>
		html={{ html }}, html|safe = {{ html|safe }} <br/>
		value3 = {{ value3 }}, value3|upper = {{ value3|upper }}, value3|slugify = {{ value3|slugify }}<br/>
	</h6>
</body>
</html>

執行結果:
開啟瀏覽器,輸入http://127.0.0.1:8000/filter/。


沒有留言:

張貼留言