目錄
方式二、使用render(request,'index.html') 的方式渲染
一、模板介紹
視圖函數只是返回文本,而實際上網頁大多是有樣式和HTML代碼的,模板就是用來渲染、顯示視圖的,相當於HTML的預編譯器
市面上有的模板系統,最知名的就是DTL和Jinja2,DTL是和Python無縫對接的框架
注:在Pycharm環境下,DTL的模型HTML文件中:
1.所有的標籤輸入標籤名,點擊Tab鍵,就會自動出來格式,比如輸入if,自動就會出來
{% if %}
{% endif %}
2.按Ctrl+/就可以註釋掉代碼
二、模板渲染的常用方式:
方式一、render_to_string渲染
1.新建的項目中的urls.py做url和函數視圖的映射:
from django.contrib import admin
from django.urls import path
from front import views
urlpatterns = [
#path('admin/', admin.site.urls),
path('', views.index),
]
2. #新建的應用中的views.py
from django.template.loader import render_to_string
from django.http import HttpResponse
# Create your views here.
def index(request):
html = render_to_string("index.html")
return HttpResponse(html)
3. #在templates文件中新建一個index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
這是從模板渲染的字符串
</body>
</html>
方式二、使用render(request,'index.html') 的方式渲染
from django.shortcuts import render
def index(request):
#html = render_to_string("index.html")
#return HttpResponse(html)
return render(request,'index.html')
三、模板的路徑及查找順序
1.默認DIR爲他首先尋找的路徑,如果DIR爲空,則尋找APP中的模板
這是setting.py裏的模板路徑設置
2.要想使用APP中的模板,需要滿足
- 在該APP中要有templates文件
- 該APP在setting.py裏邊的INSTALL_APP裏已經安裝
- APP_DIRS設置爲TRUE
3.當在DIRS默認的項目模板和自己應用的模板都找不到模板文件時,就會去其他應用找模板文件
四、DTL模板語法
-
1.模板變量
context參數是一個字典,對應的鍵值對中,值可以是字符,如下:
from django.shortcuts import render
def index(request):
context = {
'song_name':'someone like you'
}
return render(request, 'index.html', context=context)
#templates裏的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ song_name }}
</body>
</html>
對應的值也可以是對象,在HTML裏需要用“ . ”連接,如下
對應的值也可以是一個字典,如下:
song_name.
對應的值也可以是一個列表、元組,如下
song_name.0
-
2.常用的模板標籤
1. if標籤:
if標籤的判斷運算和Python中是一樣的,“==,>,<,!=,,,,”
如:在views.py裏定義了一個年齡的參數,通過渲染傳給模板讓其判斷是否成年
def index(request):
context = {
'age': 18
}
return render(request, 'index.html', context=context)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if age < 18 %}
<p>您是未成年人</p>
{% elif age == 18 %}
<p>您剛滿18歲</p>
{% else %}
<p>您已經成年</p>
{% endif %}
</body>
</html>
2.for..in標籤
- 1.列表的遍歷:
from django.shortcuts import render
# Create your views here.
def index(request):
context = {
'songs':[
"Rolling in the deep",
"Someone like you",
"Set fire to the rain",
]
}
return render(request, 'index.html', context=context)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
{% for song in songs %}
<li>{{ song }}</li>
{% endfor %}
</ul>
</body>
</html>
- 2.字典的遍歷
在veiws.py的context中,加一個songdetail的字典
context = {
'songs':[
'Rolling in the deep',
'someone like you',
'set fire to the rain',
],
'songdetail':{
'songname': 'Rolling in the deep',
'singer': 'Adele',
'album':'21'
}
}
在模板的index.html中分別遍歷鍵key和元組item
<ul>
{% for key in songdetail.keys %}
<li>{{ key }}</li>
{% endfor %}
</ul>
<ul>
{% for key,value in songdetail.items %}
<li>{{ key }}/{{ value }}</li>
{% endfor %}
</ul>
- 3.表格:列表中元組的遍歷——table的使用:
在views.py中的def函數的列表裏定義了字典
def index(request):
context = {
'songs': [
{
'songname':'Rolling in the deep',
'singer':'adele',
'album':'21',
},
{
'songname': 'Hello',
'singer': 'adele',
'album': '25',
},
{
'songname': 'One and Only',
'singer': 'adele',
'album': '19',
}
]
}
在項目的模板的index.html中,利用表格遍歷,thead是對錶頭的定義,tbody是對錶格內容的定義,forloop.counter0是顯示列表中第幾個元組的序號,且是從0開始
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<thead>
<tr>
<td>序號</td>
<td>歌名</td>
<td>歌手</td>
<td>專輯</td>
</tr>
</thead>
<tbody>
{% for song in songs %}
<tr>
<td>{{ forloop.counter0 }}</td>
<td>{{ song.songname }}</td>
<td>{{ song.singer }}</td>
<td>{{ song.album }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
forloop.counter的其他使用方法:
3.with標籤
用途:假如在顯示列表內容的時候,需要用“列表名.序號”的形式,有些冗雜,可以用with爲“列表名.序號”起個別名,在使用時也直接使用別名
假設有列表persons,第一個元組即是person.0,獲取其內容可以給它取個別名
'with'使用第一種方式是
{% with persons.0 as zs %}
<p>zs</p>
<p>zs</p>
{% endwith %}
第二種是
{% with zs=persons.0 %}
<p>zs</p>
<p>zs</p>
{% endwith %}
4.verbatim標籤
在DTL模板中,默認是會解析那些特殊字符或標籤的,如{%%},但是如果不想讓它解析,就可以用此標籤
參考教程:https://www.bilibili.com/video/av93363026?p=30