解決django中form表單設(shè)置action后無(wú)法回到原頁(yè)面的問(wèn)題
django中form表單設(shè)置action后,點(diǎn)提交按鈕是跳轉(zhuǎn)到action頁(yè)面的,比如設(shè)置action為login,網(wǎng)址為192.168.1.128,跳轉(zhuǎn)后便會(huì)來(lái)到192.168.1.128/login,F(xiàn)5刷新也會(huì)是重新提交表單對(duì)話(huà)框,無(wú)法回到原頁(yè)面。
因此就要在django服務(wù)器進(jìn)行重定向,具體就是
from django.shortcuts import redirect#最后返回原頁(yè)面return redirect(url)
補(bǔ)充知識(shí):Django + Ajax發(fā)送POST表單,并將返回信息回顯到頁(yè)面中
將表單數(shù)據(jù)發(fā)送回后端,然后處理后端返回的信息并顯示在當(dāng)前頁(yè)面中,這里使用Ajax進(jìn)行處理;
那么先看js代碼:
<!--以下為 Ajax腳本 --> <script src='http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'></script> <script type='text/javascript'> $(document).ready(function(){ $('#save').click(function(){ $.ajax({ url:'/api/add_event/', #url type: 'POST',#提交表單的類(lèi)型,相當(dāng)于method='post' dataType: 'json', #dataType, 這個(gè)是請(qǐng)求后,返回的數(shù)據(jù)將以json格式顯示 data:{'name': $('#id_name').val(), #在'#'號(hào)后面是控件id, 所以千萬(wàn)不要搞錯(cuò)了,要不然會(huì)出大事的'limit':$('#id_limit').val(),'address': $('#id_address').val(),'start_time': $('#id_start_time').val(),'status': $('#id_status').val(), },#Data這個(gè)地方,必須要獲取數(shù)據(jù),代表將獲取到的數(shù)據(jù)發(fā)送到后端,后端再進(jìn)行處理 success:function(data){ console.log(data); #調(diào)試使用 console.log(data.status); #調(diào)試使用 console.log(data.message); #調(diào)試使用 $('.text').text(data.message); #將后端返回到結(jié)果通過(guò)前端頁(yè)面進(jìn)行展示 }, #注意標(biāo)點(diǎn) }); #需要注意標(biāo)點(diǎn)符號(hào),如果標(biāo)點(diǎn)符合錯(cuò)誤了,那ajax基本上都不會(huì)執(zhí)行(否則,后果很?chē)?yán)重哦) }); #注意標(biāo)點(diǎn) }); #注意標(biāo)點(diǎn) </script>
注意(踩過(guò)的坑):
1.contentType: 'application/json' ——>加入該語(yǔ)句時(shí),在后端print(request.POST)時(shí)無(wú)法獲取內(nèi)容,相當(dāng)于后端根本拿不到數(shù)據(jù)。因此在網(wǎng)上搜索了解到,使用contentType: “application/json”則data只能是json字符串;不使用時(shí)contentType一般為默認(rèn)的application/x-www-form-urlencoded格式, 因此如果不限制 POST格式,干脆就不寫(xiě)。
2. 說(shuō)說(shuō)“data”這里面需要注意:data:{'name', $('#id_name').val(), } 這其中id_name必須為控件的id 名稱(chēng),使用其它的則不能獲取的數(shù)據(jù),這個(gè)還是得注意。
3. 標(biāo)注符號(hào),標(biāo)點(diǎn)符號(hào),標(biāo)點(diǎn)符號(hào),重要的事情說(shuō)三遍,當(dāng)然可以借助專(zhuān)門(mén)的編輯器(我主要是懶哦,哈哈)
4. $('.text').text(data.message); 回顯在html中,是對(duì)后端返回的數(shù)據(jù)進(jìn)行處理
那行回顯在網(wǎng)頁(yè)面上面
<font color='red'> <span class='text'></span> </font>
以下為html代碼
<div class='container'> <div class='col-md-4 col-md-offset-4'> <form onsubmit='return false' action='##' method='POST' class='form-horizontal'> <!--此處就是通過(guò)后端返回到前端,前端進(jìn)行展示--> <font color='red'><span class='text'></span> </font> <div class='form-group'><label for='id_name'>發(fā)布會(huì)名稱(chēng):</label><input type='text' name='name' placeholder='發(fā)布會(huì)名稱(chēng)' maxlength='128' required /> </div> <div class='form-group'><label for='id_limit'>Limit:</label><input type='number' name='limit' required /> </div> <div class='form-group'><label for='id_address'>發(fā)布會(huì)地址:</label><input type='text' name='address' placeholder='地址' maxlength='128' required /> </div> <div class='form-group'><label for='id_start_time'>開(kāi)始日期:</label><input type='text' name='start_time' required /> </div> <div class='form-group'><label for='id_status'>發(fā)布狀態(tài):</label><select name='status' id='id_status'> <option value='blank'>-----</option> <option value='1'>True</option> <option value='0'>False</option></select> </div> <div align='center'> <input type='submit' value='保存發(fā)布會(huì)' ></input > </div> </form> </div></div>
現(xiàn)在來(lái)看一下后端的代碼:
from django.views.decorators.csrf import csrf_exempt@csrf_exemptdef add_event(request): if request.is_ajax(): print(request.body) print(request.POST) name = request.POST.get(’name’, ’’) # 發(fā)布會(huì)名稱(chēng) limit = request.POST.get(’limit’, ’’) # 限制人員 status = request.POST.get(’status’, ’’) # 發(fā)布會(huì)狀態(tài) address = request.POST.get(’address’, ’’) # 發(fā)布會(huì)地址 start_time = request.POST.get(’start_time’, ’’) # 發(fā)布會(huì)時(shí)間 if name == ’’ or limit == ’’ or status == ’’ or start_time == ’’: return JsonResponse({’status’: 10021, ’message’: ’parameter error’}) # 判斷發(fā)布會(huì)名稱(chēng)重復(fù) result = Event.objects.filter(name=name) if result: return JsonResponse({’status’: 10023, ’message’: ’event name already exists’}) if status == ’’: status = 1 try: # Event.objects.create(id = eid, name = name, limit = limit, address = address, status = int(status), start_time=start_time) Event.objects.create(name=name, limit=limit, address=address, status=int(status), start_time=start_time) except ValidationError as e: error = ’start_time format error. It must be in YYYY-MM-DD HH:MM:SS’ return JsonResponse({’status’: 10024, ’message’: error}) return JsonResponse({’status’: 200, ’message’: ’add event success’})
1、在后端處理時(shí),我們需要加入:@csrf_exempt 標(biāo)記,所以導(dǎo)包from django.views.decorators.csrf import csrf_exempt,否則會(huì)出現(xiàn)錯(cuò)誤csrf_token錯(cuò)誤 (403)
2、request.is_ajax()判斷當(dāng)前是否是使用ajax 進(jìn)行表單提交
3、django request.POST / request.body
當(dāng)request.POST沒(méi)有值 需要考慮:
1.請(qǐng)求頭中的: Content-Type: application/x-www-form-urlencoded request.POST中才會(huì)有值(才會(huì)去request.body中解析數(shù)據(jù)),關(guān)于Content-Type前面也提到,不寫(xiě)的錯(cuò)誤,它就是默認(rèn)。
request.body的請(qǐng)求數(shù)據(jù)
b’name=%E5%A4%BA%E5%A4%BA&limit=123‘
request.POST的數(shù)據(jù),django已進(jìn)行自動(dòng)處理
QueryDict: {‘name’: [‘奪奪’], ‘limit’: [‘123’]
以上這篇解決django中form表單設(shè)置action后無(wú)法回到原頁(yè)面的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp在iis7報(bào)錯(cuò)行號(hào)不準(zhǔn)問(wèn)題的解決方法2. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介3. 淺談CSS不規(guī)則邊框的生成方案4. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享5. 5個(gè)HTML5的常用本地存儲(chǔ)方式詳解與介紹6. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法7. html中的form不提交(排除)某些input 原創(chuàng)8. 詳解盒子端CSS動(dòng)畫(huà)性能提升9. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……10. CSS百分比padding制作圖片自適應(yīng)布局
