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í)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)發(fā)送帶附件的郵件代碼分享
在本篇文章里小編給大家整理的是關(guān)于python實(shí)現(xiàn)發(fā)送帶附件的郵件代碼分享內(nèi)容,需要的朋友們可以參考下。2020-09-09
使用Python的pencolor函數(shù)實(shí)現(xiàn)漸變色功能
這篇文章主要介紹了使用Python的pencolor函數(shù)實(shí)現(xiàn)漸變色功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python os.mkdir()與os.makedirs()的使用區(qū)別
這篇文章主要介紹了Python os.mkdir()與os.makedirs()的使用區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
python字典各式各樣操作從基礎(chǔ)到高級(jí)全面示例詳解
在Python中,字典(Dictionary)是一種強(qiáng)大而靈活的數(shù)據(jù)結(jié)構(gòu),它允許你存儲(chǔ)和檢索鍵值對(duì),本文將深入探討Python中各式各樣的字典操作,包括基本操作、高級(jí)操作以及一些實(shí)用的技巧,通過全面的示例代碼,將展示如何充分發(fā)揮字典在Python編程中的優(yōu)勢(shì)2023-12-12
python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

