在上一章你可能已經(jīng)發(fā)現(xiàn)了這樣的問題,就是在視圖返回文本的時(shí)候,HTML代碼被硬編碼在了python的代碼中。如%s等等。像這樣寫往往使得程序更加復(fù)雜,一旦修改起來又顯得十分的麻煩,而且HTML代碼程序員不見得會(huì)python代碼,現(xiàn)在的開發(fā)一般都會(huì)使得HTML前臺(tái)頁面和Python后臺(tái)分離,也就是前臺(tái)只負(fù)責(zé)顯示頁面,后臺(tái)只負(fù)責(zé)處理數(shù)據(jù)和其他操作。因此,模板顯得尤為重要。
那么,什么是模板呢?
模板是一個(gè)文本,用于分離文檔的表現(xiàn)形式和內(nèi)容。 模板定義了占位符以及各種用于規(guī)范文檔該如何顯示的各部分基本邏輯(模板標(biāo)簽)。 模板通常用于產(chǎn)生HTML,但是Django的模板也能產(chǎn)生任何基于文本格式的文檔。下面我們從一個(gè)簡單的例子來學(xué)習(xí)下什么是模板。(這個(gè)例子源自DjangoBook2)
Ordering notice Ordering notice
Dear {{ person_name }},
Thanks for placing an order from {{ company }}. It's scheduled to ship on {{ ship_date|date:"F j, Y" }}.
Here are the items you've ordered:
{% for item in item_list %}
Your warranty information will be included in the packaging.
You didn't order a warranty, so you're on your own when the products inevitably stop working.
{% endif %}Sincerely,
{{ company }}
如上所示用{{...}}或者{%...%}來替代python代碼的方式就是模板,像第一個(gè){{person_name}}其實(shí)就是一個(gè)變量,而{%for....%}或者{% if ...%}等就是循環(huán)。先不去深究上面的代碼的意思,我們下面一步一步來學(xué)習(xí)怎么使用它。
>>> from django import template >>> t = template.Template('My name is {{ name }}.') >>> c = template.Context({'name': 'Adrian'}) >>> print(t.render(c)) My name is Adrian. >>> c = template.Context({'name': 'Fred'}) >>> print(t.render(c)) My name is Fred.
當(dāng)你看到上面的代碼時(shí)你可能會(huì)急不可耐的去嘗試,結(jié)果在第二行卻出現(xiàn)了錯(cuò)誤。一般來說唯一可能出現(xiàn)的錯(cuò)誤就是:'DJANGO_SETTINGS_MODULE'error,這是因?yàn)镈jango搜索DJANGO_SETTINGS_MODULE環(huán)境變量時(shí),它被設(shè)置在settings.py中,而直接啟動(dòng)python shell就會(huì)導(dǎo)致它不知道用哪個(gè)配置文件。例如,假設(shè)mysite在你的Python搜索路徑中,那么DJANGO_SETTINGS_MODULE應(yīng)該被設(shè)置為:’mysite.settings’。所以為了免去設(shè)置環(huán)境變量的麻煩,我們應(yīng)該這樣啟動(dòng)python shell。
python manage.py shell
這樣可以免去你大費(fèi)周章地去配置那些你不熟悉的環(huán)境變量。
下面我們來分析下那段代碼。
>>> from django import template #從django中導(dǎo)入template對象 >>> t = template.Template('My name is {{ name }}.') #使用template對象的Template()方法 >>> c = template.Context({'name': 'Adrian'}) #使用template對象的Context()函數(shù)給賦值,比如name的值就是Adrian,Context()的()里面是一個(gè)字典 >>> print(t.render(c)) #渲染模板,也就是講Context賦值后的name的值A(chǔ)drian替換上面Template()中的{{name}}并
從上面的例子可以看出,使用模板的三步。一、調(diào)用Template函數(shù);二、調(diào)用Context函數(shù);三、調(diào)用render函數(shù)。就這么簡單。
下面我們再通過幾個(gè)代碼來說說Context()函數(shù)。
#代碼段1: >>> from django.template import Template,Context >>> t=Template('hello,{{name}}') >>> for name in ('A','B','C'): ... print(t.render(Context({'name':name}))) ... hello,A hello,B hello,C #代碼段2: >>> from django.template import Template,Context >>> person={'name':'Thunder','age':'108'} >>> t=Template('{{person.name}} is {{person.age}} years old!') >>> c=Context({'person':person})#后面的這個(gè)person是一個(gè)字典 >>> t.render(c) 'Thunder is 108 years old!' #代碼段3: >>> from django.template import Template,Context >>> t=Template('Item 2 is {{items.2}}')#items.2的意思是調(diào)用items列表的第3個(gè)元素,因?yàn)榱斜淼乃饕菑?開始的 >>> c=Context({'items':['Apple','Banana','Orange']}) >>> t.render(c) 'Item 2 is Orange'
注意:上面的items.2不能是items.-1或者其他什么負(fù)數(shù)索引。
好好觀察上面三段代碼,是不是就舉一反三了呢?另外默認(rèn)情況下,如果一個(gè)變量不存在,模板系統(tǒng)會(huì)把它展示為空字符串,不做任何事情來表示失敗。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。