Django(part11)--MTV模式及模板
學習筆記,僅供參考
文章目錄
- Django的框架模式
- MTV模式
- 模板Template
- 什么是模板
- 模板的配置
- 創建一個新的項目
- sittings.py文件
- 添加路徑
- 編寫模板
- 加載模板
- 模板的傳參
- 編寫第二個模板
- 使用loader加載模板并傳參
- 使用render直接加載模板并傳參
- 模板的變量
- 舉個例子
Django的框架模式
MTV模式
MTV模式MTV代表Model-Template-View(模型一模板一視圖)模式。這種模式用于應用程序的分層開發
- 作用
- 降低模塊的耦合度
- MTV
- M–模型層(Model)負責與數據庫交互;
- T–模板層(Template)負責生成 HTML,呈現內容到瀏覽器;
- V–視圖層(View)負責處理業務邏輯,負責接收請求、獲取數據,返回結果;
- 圖示
模板Template
什么是模板
- 模板是HTML頁面,可以根據視圖中傳遞的數據填充值相應的頁面元素。
- 模板層提供了一個對設計者友好的語法用于渲染向用戶呈現的信息。
模板的配置
創建一個新的項目
為了方便演示,我們創建一個新項目mywebsite2:
在項目下創建一個模板文件夾templates(這個文件夾用于存放模板文件):
sittings.py文件
在settings.py中有一個TEMPLATES列表,我們可以在這個列表中對模板進行設定。比如:
BACKEND:指定模板的引擎 DIRS:指定保存模板的目錄們 APP_DIRS:是否要在應用中搜索模板的版本 OPTIONS:有關模板的選項們我們看一下settings.py
TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},}, ]現在,我們開始配置模板!
添加路徑
首先,我們把模板文件夾的路徑添加到DIRS中:
'DIRS': [os.path.join(BASE_DIR, 'templates')],其中BASE_DIR是全局變量,是當前項目的絕對路徑。
編寫模板
首先,我們在templates文件夾下,創建模板文件page1.html:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Template</title> </head> <body><h1>這是我的第一個模板</h1></body> </html>我們再創建一個views.py文件:
from django.http import HttpResponsedef page1_template(request):return HttpResponse("OK!")在urls.py文件中,添加一個路由:
from django.contrib import admin from django.urls import path from django.urls import re_path from . import viewsurlpatterns = [path('admin/', admin.site.urls),re_path(r'page1_template/$', views.page1_template), ]這時,我們還沒有加載模板,只是編寫好了模板文件。
加載模板
- 第一種加載模板的方式
在視圖函數中,通過loader方法獲取模板,再通過HttpResponse進行相響應。
views.py
from django.http import HttpResponse #導入loader from django.template import loaderdef page1_template(request):#通過loader加載模板t = loader.get_template("page1.html")#將t轉換為字符串html = t.render()#響應return HttpResponse(html)向http://127.0.0.1:8000/page1_template/發起請求:
- 第二種加載模板的方式
使用render直接加載并響應模板
views.py:
def test_page1_template(request):from django.shortcuts import renderreturn render(request, "page1.html")注意,這里的render方法,返回的依然是HttpResponse對象。
urls.py
urlpatterns = [path('admin/', admin.site.urls),re_path(r'test_page1_template/$', views.test_page1_template), ]向http://127.0.0.1:8000/test_page1_template/發起請求:
模板的傳參
模板傳參是指把數據形成字典,傳參給模板,由模板渲染來填充數據
編寫第二個模板
為了方便之后的學習,我們再寫一個模板文件page2.html:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Template</title> </head> <body><h1>這是我的第2個模板</h1><h2> 姓名:{{name}}</h2><h2>年齡:{{age}}</h2><!--模板填入數據的格式{{變量名}}--></body> </html>urls.py
urlpatterns = [path('admin/', admin.site.urls),re_path(r'test_page1_template/$', views.test_page1_template),re_path(r'page2_template/$', views.page2_template) ]使用loader加載模板并傳參
views.py
def page2_template(request):t = loader.get_template("page2.html")html = t.render({"name":"Huang","age":10})return HttpResponse(html)注意,render方法中只接受字典形式的數據。
向http://127.0.0.1:8000/page2_template/發起請求:
使用render直接加載模板并傳參
views.py
def page2_template(request):d = {"name":"Bai","age":11} return render(request, "page2.html", d)再次向http://127.0.0.1:8000/page2_template/發起請求:
模板的變量
- 在模板中使用變量語法
后端中,必須將變量封裝到字典中才允許傳遞到模板上,其中鍵必須是字符串,值可以是列表,字典,自定義對象等等:
dic = {"變量1":值1, "變量2":值2}舉個例子
page2.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Template</title> </head> <body><h1>這是我的第2個模板</h1><h2> 姓名:{{name}}</h2><h2>年齡:{{age}}</h2><h3>興趣:{{hobby}}</h3><h3>第1個興趣:{{hobby.0}}</h3><h3>朋友:{{friend.name}}</h3><h3>兔子:{{bunny.name}}</h3><h3>兔子說:{{bunny.speak}}</h3></body> </html>urls.py
from django.contrib import admin from django.urls import path from django.urls import re_path from . import viewsurlpatterns = [path('admin/', admin.site.urls),re_path(r'page2_template/$', views.page2_template), ]views.py
class Bunny:def __init__(self, name, age):self.name = nameself.age = agedef speak(self):string = self.name + "今年已經" + str(self.age) + "個月啦"return stringdef page2_template(request):d = {"name":"Ada","age":22,"hobby":["羽毛球", "Game", "看書"],"friend":{"name":"Wang", "age":23},"bunny":Bunny("Huang", 10)}return render(request, "page2.html", d)向http://127.0.0.1:8000/page2_template/發起請求:
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Django(part11)--MTV模式及模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 挖孔屏是什么意思(什么叫做挖孔屏)
- 下一篇: Django(part12)--模板的标