Django 后臺(tái)帶有字典的列表數(shù)據(jù)與頁面js交互實(shí)例
1、這里只是簡(jiǎn)單介紹一下Django的view如何跟js進(jìn)行交互,首先,進(jìn)入用戶明細(xì)的時(shí)候會(huì)進(jìn)入一個(gè)頁面,叫用戶信息表,里面包含了用戶學(xué)習(xí)的課程和所得到的分?jǐn)?shù),每門課程對(duì)應(yīng)一個(gè)分?jǐn)?shù),其中課程用下拉框依次顯示,選擇課程時(shí)動(dòng)態(tài)顯示課程的分?jǐn)?shù),django view部分代碼如下:
def user_info(request, userid): if request.method == 'GET': user = User.objects.get(userid=userid) user_info = UserInfo.objects.get(userid=userid) content = {'user': user, 'user_info': user_info} detail_data = {} data = [] for detail in user_info: detail_data[’course’] = detail.course detail_data[’score’] = str(detail.score) data.append(json.dumps(detail_data, ensure_ascii=False)) content[’detail’] = data return render(request, 'user/user_info/user_info.html', content)
其中,需注意的是下面這段代碼,
(1)、定義一個(gè)空的字典為detail_data,接著再定義一個(gè)空的列表data,循環(huán)得到每個(gè)用戶信息的詳情,也就是用戶的每個(gè)課程對(duì)應(yīng)的每個(gè)分?jǐn)?shù),分別把值添加進(jìn)字典里面去。
(2)、后面在把字典的值通過json.dumps轉(zhuǎn)換為json格式,這樣才能給html頁面的js進(jìn)行交互,而且如果有中文的話,需要在后面加個(gè)ensure_ascii=False參數(shù),不然的話js得到的數(shù)據(jù)不是我們想得到的數(shù)據(jù)。
(3)、最后,再把轉(zhuǎn)成json的字典數(shù)據(jù)添加進(jìn)列表data中,最后通過content[’detail’]=data把這個(gè)列表傳到頁面上,供js調(diào)用。
detail_data = {} data = [] for detail in user_info: detail_data[’course’] = detail.course detail_data[’score’] = str(detail.score) data.append(json.dumps(detail_data, ensure_ascii=False)) content[’detail’] = data
2、接下來看下html中如何處理上面?zhèn)鬟^的detail數(shù)據(jù),其中課程用下拉框依次顯示,選擇課程時(shí)動(dòng)態(tài)顯示課程的分?jǐn)?shù),代碼如下:
<script> function select() { var course =$(’#course option:selected’).val(); var details = {{ detail|safe }} for(var detail in details){ var data = JSON.parse(details[detail]); if(course == data.course){ $(’#score’).html(data.score); } } } </script>
代碼解析一下:
(1)、其中獲取下拉框選擇的課程值,賦給一個(gè)變量course,接著把傳過來界面的detail,賦給一個(gè)變量details,注意這里必須要用{{ detail|safe }},不然取出來的數(shù)據(jù)會(huì)不是想要的。
(2)、接著,循環(huán)上面得到的變量,也就是一個(gè)帶有字典的列表,循環(huán)就得到每一個(gè)帶有課程和課程分?jǐn)?shù)的字典,因?yàn)樵趘iew底下是把每一個(gè)字典轉(zhuǎn)換為json格式,所以現(xiàn)在必須把循環(huán)得到每一個(gè)字典通過json解析得到其對(duì)應(yīng)的,通過JSON.parse(details[detail]),否則也是取不到對(duì)應(yīng)的數(shù)據(jù)。
(3)、通過頁面下拉框選擇的課程值,跟取到的每個(gè)課程的分?jǐn)?shù)做比較,相等的話,就取出對(duì)應(yīng)課程的分?jǐn)?shù),填充進(jìn)頁面中。
3、Django和js交互的網(wǎng)上例子太少,這里積累一下,以上內(nèi)容僅供學(xué)習(xí)參考,謝謝!主要還是自己去嘗試。
補(bǔ)充知識(shí):django 后臺(tái)數(shù)據(jù)直接交給頁面
<html><head> <title>運(yùn)維平臺(tái)</title> <link rel='stylesheet' type='text/css' href='http://www.piao2010.com/static/Css/Monitor/addmqmonitor.css' rel='external nofollow' > <link rel='stylesheet' type='text/css' href='http://www.piao2010.com/static/Css/Public/header.css' rel='external nofollow' rel='external nofollow' > <link rel='stylesheet' type='text/css' href='http://www.piao2010.com/static/Css/Public/menu.css' rel='external nofollow' rel='external nofollow' ></head><body> <include file='Public:header'/> <div class='content'> <include file='Public:menu'/> <div class='con fl'> <form action='/addmqmonitor/' method='post'><label class='condition'>應(yīng)用</label><input type='text' name='app' class='equipment_sz'> <label class='condition'>隊(duì)列管理器</label><input type='text' name='qmgr' class='equipment_sz'> <label class='condition'>通道名稱</label><input type='text' name='channel' class='equipment_sz'><br /> <label class='condition'>IPADDR</label><input type='text' name='ipaddr' class='equipment_sz'> <label class='condition'>PORT</label><input type='text' name='port' class='equipment_sz'> <label class='condition'>隊(duì)列監(jiān)控閾值</label><input type='text' name='depth' class='equipment_sz'> <label class='condition'>是否監(jiān)控</label><input type='text' name='flag' class='equipment_sz'><br /> <input type='submit' value='設(shè)備添加' class='equipment_add_btn'> </form> </div> </div></body><script type='text/javascript' src='https://rkxy.com.cn/static/Js/jquery-2.2.2.min.js'></script><!-- <script type='text/javascript' src='https://rkxy.com.cn/static/Js/Equipment/addEquipment.js'></script> --></html> def addmqmonitor(req): print req.get_full_path() app= req.POST[’app’] qmgr= req.POST[’qmgr’] channel= req.POST[’channel’] ipaddr= req.POST[’ipaddr’] port= req.POST[’port’] depth= req.POST[’depth’] flag= req.POST[’flag’] conn= MySQLdb.connect( host=’127.0.0.1’, port = 3306, user=’root’, passwd=’1234567’, db =’DEVOPS’, charset='UTF8' ) cursor = conn.cursor() sql = 'insert into mon_mq(name,qmgr,channel,ipaddr,port,depth,flag) values(’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’)' % (app,qmgr,channel,ipaddr,port,depth,flag) cursor.execute(sql) conn.commit() a = cursor.execute('select name,qmgr,channel,ipaddr,port,flag from mon_mq' ) info = cursor.fetchall() print info print type(info) return render(req,’listmqinfo.html’,{’info’:info}) [root@yyjk templates]#cat listmqinfo.html <html> <head> <title>運(yùn)維平臺(tái)</title> <link rel='stylesheet' type='text/css' href='http://www.piao2010.com/static/Css/Equipment/modifyBtn.css' rel='external nofollow' > <link rel='stylesheet' type='text/css' href='http://www.piao2010.com/static/Css/Public/header.css' rel='external nofollow' rel='external nofollow' > <link rel='stylesheet' type='text/css' href='http://www.piao2010.com/static/Css/Public/menu.css' rel='external nofollow' rel='external nofollow' > </head> <table border='10'>{% for x in info %} <tr><th>{{x.0}}</th><th>{{x.1}}</th><td>{{x.2}}</td><td>{{x.3}}</td><td>{{x.4}}</td><td>{{x.5}}</td></tr>{% endfor %} </table>
以上這篇Django 后臺(tái)帶有字典的列表數(shù)據(jù)與頁面js交互實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. vue前端RSA加密java后端解密的方法實(shí)現(xiàn)3. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能4. 基于jsp+mysql實(shí)現(xiàn)在線水果銷售商城系統(tǒng)5. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)6. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享7. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享8. ASP中實(shí)現(xiàn)字符部位類似.NET里String對(duì)象的PadLeft和PadRight函數(shù)9. 利用CSS3新特性創(chuàng)建透明邊框三角10. 淺談?dòng)蓀osition屬性引申的css進(jìn)階討論

網(wǎng)公網(wǎng)安備