Django+Ajax+jQuery實(shí)現(xiàn)網(wǎng)頁動(dòng)態(tài)更新的實(shí)例
views.py中的修改
增加相應(yīng)的請(qǐng)求處理函數(shù):
def getdevjson(request): print 'get here' if ('key' in request.GET): searchkey = request.GET.get('key') return JsonResponse(search(searchkey)) else: return HttpResponse('Sorry!')
返回字符串中,既可以使用from django.http import JsonResponse,也可以使用HttpResponse(json.dumps(res))
前端網(wǎng)頁修改
<script type="text/javascript"> window.jQuery || document.write("<script src='../static/js/jquery.min.js'>" + "<" + "/script>"); </script> <script type="text/javascript"> $(function() { var submit_form = function(e) { $.ajax({ type : "GET", url : "/getdevjson?"+Math.random(), data : { key: $('#searchContent').val() }, dataType : "text", success : function(res){ $('#searchContent').focus().select(); //console.log(res); update(res); }, error : function() { alert("處理異常返回!");} }); return false; }; $('#calculate').bind('click', submit_form); $('input[type=text]').bind('keydown', function(e) { if (e.keyCode == 13) { submit_form(e); } }); $('#searchContent').focus(); }); </script>
<div class="divRight" id="divright1"> <div class="divRight" style="height:70px; width:370px;"> <label id="lblSearch" class="cssLabelSearch">請(qǐng)輸入查詢key:</label> <input id="searchContent" type="text" size="40"></input> <input id="calculate" type="button" value="確定" ></input> </div> <br> <label id="lbl1" class="cssLabelClient">節(jié)點(diǎn)信息</label> <Textarea id="ClientInfoArea" readonly class="txtClientInfo"></Textarea> </div>
#calculate是一個(gè)按鈕,點(diǎn)擊動(dòng)作綁定了提交函數(shù)submit_form,ajax的請(qǐng)求參數(shù)中,data中包含了查詢參數(shù),success是請(qǐng)求成功后的動(dòng)作,注意返回的res需要進(jìn)行json解析才可以正確使用:root = JSON.parse(jsondata);update(res)是一個(gè)更新網(wǎng)頁內(nèi)容的函數(shù)
路由配置修改
urls.py中修改如下:
from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'^getdevjson$','dev.views.getdevjson',name='getdevjson'), url(r'^','dev.views.index',name='index'), url(r'^admin/', include(admin.site.urls)), )
需要注意的是為了避免路由被覆蓋,將index的路由配置盡量放置在最后一行。
以上這篇Django+Ajax+jQuery實(shí)現(xiàn)網(wǎng)頁動(dòng)態(tài)更新的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python模擬點(diǎn)擊在ios中實(shí)現(xiàn)的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于python模擬點(diǎn)擊在ios中實(shí)現(xiàn)的實(shí)例講解內(nèi)容,有需要的朋友們可以參考下。2020-11-11matlab灰度圖像調(diào)整及imadjust函數(shù)的用法詳解
這篇文章主要介紹了matlab圖像灰度調(diào)整及imadjust函數(shù)的用法詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Python協(xié)程異步爬取數(shù)據(jù)(asyncio+aiohttp)實(shí)例
這篇文章主要為大家介紹了Python協(xié)程異步爬取數(shù)據(jù)(asyncio+aiohttp)實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08對(duì)python創(chuàng)建及引用動(dòng)態(tài)變量名的示例講解
今天小編就為大家分享一篇對(duì)python創(chuàng)建及引用動(dòng)態(tài)變量名的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11Python Web程序部署到Ubuntu服務(wù)器上的方法
在本文記錄了我在Ubuntu中部署Flask Web站點(diǎn)的過程, 其中包括用戶創(chuàng)建、代碼獲取、Python3環(huán)境的安裝、虛擬環(huán)境設(shè)置、uWSGI啟動(dòng)程序設(shè)置,并將Nginx作為前端反向代理,需要的朋友參考下吧2018-02-02Python機(jī)器學(xué)習(xí)之PCA降維算法詳解
今天帶大家復(fù)習(xí)python機(jī)器學(xué)習(xí)的知識(shí)點(diǎn),文中對(duì)PCA降維算法介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python機(jī)器學(xué)習(xí)的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05python tqdm實(shí)現(xiàn)進(jìn)度條的示例代碼
這篇文章主要介紹了python tqdm實(shí)現(xiàn)進(jìn)度條的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11