詳解Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化
優(yōu)化場(chǎng)景
利用視圖函數(shù)(views)查詢數(shù)據(jù)之后可以通過上下文context、字典、列表等方式將數(shù)據(jù)傳遞給HTML模板,由template引擎接收數(shù)據(jù)并完成解析。但是通過context傳遞數(shù)據(jù)可能就存在在不同的視圖函數(shù)中使用重復(fù)的查詢語句,所以可以通過將重復(fù)查詢語句設(shè)置全局變量,配合locals()函數(shù)進(jìn)行數(shù)據(jù)查詢與傳遞。
優(yōu)化前
def index(request):
threatname = '威脅情報(bào)展示'
url = 'www.testtip.com'
allthreat = Threat.objects.all()
#推薦位的威脅情報(bào)
rec = Threat.objects.filter(rec__id=1)[:3]
#情報(bào)標(biāo)簽
threat_tags = Tag.objects.all()
#將上述數(shù)據(jù)封裝至上下文中
context = {
'threatname': threatname,
'url': url,
'allthreat': allthreat,
'rec':rec,
'threat_tags':threat_tags,
}
#通過render傳遞上下文至模板templates
return render(request,'index.html',context)
def threatshow(request,tid):
allthreat = Threat.objects.all()
#推薦位的威脅情報(bào)
rec = Threat.objects.filter(rec__id=1)[:3]
#情報(bào)標(biāo)簽
threat_tags = Tag.objects.all()
# 熱門情報(bào)數(shù)據(jù)
hot_threat = Threat.objects.filter(hot__id=x)[:6]
#將sitename&url&allarticle封裝至上下文中
context = {
'allthreat': allthreat,
'rec':rec,
'threat_tags':threat_tags,
'hot_threat':hot_threat,
}
return render(request, 'threatshow.html',context)
上面可以看到 views 里面有 index() 和 threatshow() 兩個(gè)視圖函數(shù),在這兩個(gè)視圖函數(shù)中有三個(gè)相同的數(shù)據(jù)查詢語句:
allthreat = Threat.objects.all() #推薦位的威脅情報(bào) rec = Threat.objects.filter(rec__id=1)[:3] #情報(bào)標(biāo)簽 threat_tags = Tag.objects.all()
優(yōu)化后
設(shè)置全局變量
# 全局定義常用查詢數(shù)據(jù)參數(shù) def global_variable(request): allthreat = Threat.objects.all() #推薦位的威脅情報(bào) rec = Threat.objects.filter(rec__id=1)[:3] #情報(bào)標(biāo)簽 threat_tags = Tag.objects.all() return locals() views 中定義上述全局變量后,通過locals()函數(shù)優(yōu)化如下: def index(request): threatname = '威脅情報(bào)展示' url = 'www.testtip.com' #通過render傳遞上下文至模板templates return render(request,'index.html',locals()) def threatshow(request,tid): # 熱門情報(bào)數(shù)據(jù) hot_threat = Threat.objects.filter(hot__id=x)[:6] return render(request, 'threatshow.html',locals())
Python 中的 locals() 函數(shù)會(huì)以字典類型返回當(dāng)前位置的全部局部變量,也就是返回當(dāng)前 index() 、 threatshow() 視圖函數(shù)中定義的局部數(shù)據(jù)查詢結(jié)果,加上全局變量當(dāng)中已經(jīng)完成了其他剩余數(shù)據(jù)查詢,所以在滿足數(shù)據(jù)查詢需求的基礎(chǔ)上完成了視圖函數(shù)優(yōu)化。
到此這篇關(guān)于詳解Django中views數(shù)據(jù)查詢使用locals()函數(shù)進(jìn)行優(yōu)化的文章就介紹到這了,更多相關(guān)Django locals()優(yōu)化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python TCP全連接攻擊中SockStress全連接攻擊詳解
Sock Stress 全連接攻擊屬于TCP全連接攻擊,因?yàn)樾枰⒁淮瓮暾腡CP三次握手,該攻擊的關(guān)鍵點(diǎn)就在于,攻擊主機(jī)將windows窗口緩沖設(shè)置為0,實(shí)現(xiàn)的拒絕服務(wù)2022-10-10
python中dict字典的查詢鍵值對(duì) 遍歷 排序 創(chuàng)建 訪問 更新 刪除基礎(chǔ)操作方法
字典的每個(gè)鍵值(key=>value)對(duì)用冒號(hào)(:)分割,每個(gè)對(duì)之間用逗號(hào)(,)分割,整個(gè)字典包括在花括號(hào)({})中,本文講述了python中dict字典的查詢鍵值對(duì) 遍歷 排序 創(chuàng)建 訪問 更新 刪除基礎(chǔ)操作方法2018-09-09
numpy 計(jì)算兩個(gè)數(shù)組重復(fù)程度的方法
今天小編就為大家分享一篇numpy 計(jì)算兩個(gè)數(shù)組重復(fù)程度的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
django的安裝和創(chuàng)建應(yīng)用過程詳解
這篇文章主要介紹了django的安裝和創(chuàng)建應(yīng)用,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

