參考資料:Python架站特訓班Django 3最強實戰
延續專案:用擲骰子來說視圖(View)如何傳送變數到模版(Template)
範例一、依序顯示學生資料
1.設定show的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 | """ 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 urlpatterns = [ path('admin/', admin.site.urls), path('dice/', dice), path('dice2/', dice2), path('dice3/', dice3), path('show/', show), ] |
2.定義show()函式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 | 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())
|
3.新增templates\show.html檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>學生基本資料</title> </head> <body> <h4> {% for student in students %} <ul> <li> 姓名:{{student.name}} </li> <li> 手機:{{student.phone}} </li> <li> 年紀:{{student.age}} </li> </ul> {% empty %} 沒有任何資料 {% endfor %} </h4> </body> </html> |
執行結果:
開啟瀏覽器,輸入http://127.0.0.1:8000/show/。
執行結果:
範例二、利用forloop變量及其屬性由1開始顯示資料第幾位學生
4.修訂templates\show.html檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>學生基本資料</title> </head> <body> <h6> {% for student in students %} 第{{forloop.counter}}位學生 <ul> <li> 姓名:{{student.name}} </li> <li> 手機:{{student.phone}} </li> <li> 年紀:{{student.age}} </li> </ul> {% empty %} 沒有任何資料 {% endfor %} </h6> </body> </html> |
開啟瀏覽器,輸入http://127.0.0.1:8000/show/。
執行結果:
範例三、for迴圈倒轉
5.修訂templates\show.html檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>學生基本資料</title> </head> <body> <h6> {% for student in students reversed %} 第{{forloop.revcounter}}位學生 <ul> <li> 姓名:{{student.name}} </li> <li> 手機:{{student.phone}} </li> <li> 年紀:{{student.age}} </li> </ul> {% empty %} 沒有任何資料 {% endfor %} </h6> </body> </html> |
執行結果:
沒有留言:
張貼留言