這篇文章主要為大家展示了如何使用JavaScript獲取Django模板指定鍵值數(shù)據(jù),內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
創(chuàng)新互聯(lián)建站是一家專注于成都做網(wǎng)站、成都網(wǎng)站制作與策劃設(shè)計(jì),耿馬網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:耿馬等地區(qū)。耿馬做網(wǎng)站價(jià)格咨詢:18982081108
Django中利用js來(lái)操作數(shù)據(jù)的常規(guī)操作一般為點(diǎn)(.)操作符來(lái)獲取字典或列表的數(shù)據(jù),一般如{{data.0}},{{data.arg}}
但有時(shí)如果數(shù)據(jù)是嵌套類型的數(shù)據(jù)時(shí),直接獲取某個(gè)值就變得困難了,比如下面的格式要獲取
correct中qxl的值
startArgsSet={"correct":{"qxl":0,"kkx":0},"reliable":{"qxl":0,"kkx":0},"security":{"qxl":0,"kkx":0},"understand":{"qxl":0,"kkx":0},"entropy":{"qxl":0,"kkx":0}, "mature":{"qxl":0,"kkx":0},"active":{"qxl":0,"kkx":0},"service":{"qxl":0,"kkx":0},"file":{"qxl":0,"kkx":0}, "tech":{"qxl":0,"kkx":0},"property":{"qxl":0,"kkx":0},"organize":{"qxl":0,"kkx":0},"develop":{"qxl":0,"kkx":0}, "source":{"qxl":0,"kkx":0},"update":{"qxl":0,"kkx":0},"fix":{"qxl":0,"kkx":0}, "quality":{"qxl":0,"kkx":0},"meanNum":{"qxl":0,"kkx":0},"variance":{"qxl":0,"kkx":0} }
這時(shí)候如果改變數(shù)據(jù)格式為列表中的無(wú)嵌套字典格式比較麻煩,可以自定義過(guò)濾器來(lái)獲取數(shù)據(jù),
from django.template.defaulttags import register @register.filter def getArgQxlValue(dictionary,arg): return dictionary[arg]['qxl'] @register.filter def getArgKkxValue(dictionary,arg): return dictionary[arg]['kkx'] @register.filter def getArgName(dictionary,arg): return dictionary[arg]['name']
在模板中就可以以這樣寫
{% load staticfiles %}超標(biāo)信息 {{ guestSetArgs|safe }}
{{ warningdata|safe }}
形如{{warningdata|getArgQxlValue:j}},就可以獲取到里面的值了
稍做美化
以上就是關(guān)于如何使用JavaScript獲取Django模板指定鍵值數(shù)據(jù)的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。