一,get_template 函數(shù)
成都創(chuàng)新互聯(lián)公司專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,四川電信機(jī)房托管,四川電信機(jī)房托管,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。
from django.template.loader import get_template
from django.http import HttpResponse
import datetime
def temp_test(request):
now = datetime.datetime.now()
t = get_template('temp_test.html')
html = t.render({'current_date': now})
return HttpResponse(html)
模板輸出:
It is now {{ current_date }}
get_template() 函數(shù)以模板名稱為參數(shù),在文件系統(tǒng)中找出模塊的位置,打開文件并返回一個(gè)編譯好的Template 對(duì)象
二,render_to_response() 渲染模板
from django.shortcuts import render_to_response
import datetime
def current_datetime(request):
now = datetime.datetime.now()
return render_to_response('current_datetime.html', {'current_date': now})
render_to_response() 的第一個(gè)參數(shù)必須是要使用的模板名稱。 如果要給定第二個(gè)參數(shù),那么該參數(shù)必須是為該模板創(chuàng)建 Context 時(shí)所使用的字典。 如果不提供第二個(gè)參數(shù), render_to_response() 使用一個(gè)空字典。
三,locals() 技巧
from django.shortcuts import render_to_response
import datetime
def temp_test(request):
now = datetime.datetime.now()
return render_to_response('temp_test.html', locals())
locals() 的值,它囊括了函數(shù)執(zhí)行到該時(shí)間點(diǎn)時(shí)所定義的一切變量
此時(shí)對(duì)應(yīng)html輸出的格式應(yīng)該為:
It is now {{ now }}
四,get_template()中使用子目錄
from django.shortcuts import render_to_response
import datetime鄭州婦科醫(yī)院 http://www.sptdfk.com/
def temp_test(request):
now = datetime.datetime.now()
return render_to_response('member/temp_test.html', locals())
只需在調(diào)用 get_template() 時(shí),把子目錄名和一條斜杠添加到模板名稱之前
由于 render_to_response() 只是對(duì) get_template() 的簡(jiǎn)單封裝, 你可以對(duì) render_to_response() 的第一個(gè)參數(shù)做相同處理
五,模板包含和模板繼承
1,模板包含:include(); 公共頭部,公共尾部,以當(dāng)前模板目錄路徑為準(zhǔn)
{% include 'common/nav.html' %}
It is now {{ now }}
2,模板繼承:block 用法
概念:模板繼承就是先構(gòu)造一個(gè)基礎(chǔ)框架模板,而后在其子模板中對(duì)它所包含站點(diǎn)公用部分和定義塊進(jìn)行重載
My helpful timestamp site
{% block content %}{% endblock %}
{% block footer %}
Thanks for visiting my site.
{% endblock %}
所有的 {% block %} 標(biāo)簽告訴模板引擎,子模板可以重載這些部分。 每個(gè) {% block %} 標(biāo)簽所要做的是告訴模板引擎,該模板下的這一塊內(nèi)容將有可能被子模板覆蓋。
則子模板就可以更改為:
{% extends "base.html" %}
{% block title %}The current time{% endblock %}
{% block content %}
It is now {{ current_date }}.
{% endblock %}
功能核心點(diǎn):
1,如果在模板中使用 {% extends %} ,必須保證其為模板中的第一個(gè)模板標(biāo)記。 否則,模板繼承將不起作用。
2,一般來(lái)說(shuō),基礎(chǔ)模板中的 {% block %} 標(biāo)簽越多越好。 記住,子模板不必定義父模板中所有的代碼塊,因此可以用合理的缺省值對(duì)一些代碼塊進(jìn)行填充,然后只對(duì)子模板所需的代碼塊進(jìn)行(重)定義
3,如果需要訪問父模板中的塊的內(nèi)容,使用 {{ block.super }} 這個(gè)標(biāo)簽,這一個(gè)魔法變量將會(huì)表現(xiàn)出父模板中的內(nèi)容。 如果只想在上級(jí)代碼塊基礎(chǔ)上添加內(nèi)容,而不是全部重載,該變量就顯得非常有用。
4,不允許在同一個(gè)模板中定義多個(gè)同名的 {% block %} 。
5,{% extends %} 對(duì)所傳入模板名稱使用的加載方法和 get_template() 相同。 也就是說(shuō),會(huì)將模板名稱被添加到 TEMPLATE_DIRS 設(shè)置之后。
6,多數(shù)情況下, {% extends %} 的參數(shù)應(yīng)該是字符串,但是如果直到運(yùn)行時(shí)方能確定父模板名,這個(gè)參數(shù)也可以是個(gè)變量
網(wǎng)站名稱:Django基礎(chǔ)知識(shí)之模板配置和輸出
URL分享:http://m.kartarina.com/article20/piphco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、標(biāo)簽優(yōu)化、ChatGPT、微信小程序、營(yíng)銷型網(wǎng)站建設(shè)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)