django中使用POST方法獲取POST數(shù)據(jù)
在django中獲取post數(shù)據(jù),首先要規(guī)定post發(fā)送的數(shù)據(jù)類型是什么。
1.獲取POST中表單鍵值數(shù)據(jù)
如果要在django的POST方法中獲取表單數(shù)據(jù),則在客戶端使用JavaScript發(fā)送POST數(shù)據(jù)前,定義post請求頭中的請求數(shù)據(jù)類型:
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
在django的views.py相關方法中,需要通過request.POST獲取表單的鍵值數(shù)據(jù),并且可以通過reques.body獲取整個表單數(shù)據(jù)的字符串內(nèi)容
if(request.method == 'POST'):
print("the POST method")
concat = request.POST
postBody = request.body
print(concat)
print(type(postBody))
print(postBody)
相關日志:
the POST method
<QueryDict: {u'username': [u'abc'], u'password': [u'123']}>
<type 'str'>
username=abc&password=123
2.獲取POST中json格式的數(shù)據(jù)
如果要在django的POST方法中獲取json格式的數(shù)據(jù),則需要在post請求頭中設置請求數(shù)據(jù)類型:
xmlhttp.setRequestHeader("Content-type","application/json");
在django的views.py中導入python的json模塊(import json),然后在方法中使用request.body獲取json字符串形式的內(nèi)容,使用json.loads()加載數(shù)據(jù)。
if(request.method == 'POST'):
print("the POST method")
concat = request.POST
postBody = request.body
print(concat)
print(type(postBody))
print(postBody)
json_result = json.loads(postBody)
print(json_result)
相關日志:
the POST method
<QueryDict: {}>
<type 'str'>
{"sdf":23}
{u'sdf': 23}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
淺談python中統(tǒng)計計數(shù)的幾種方法和Counter詳解
今天小編就為大家分享一篇淺談python中統(tǒng)計計數(shù)的幾種方法和Counter詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
python調(diào)用系統(tǒng)ffmpeg實現(xiàn)視頻截圖、http發(fā)送
這篇文章主要為大家詳細介紹了python調(diào)用系統(tǒng)ffmpeg實現(xiàn)視頻截圖、http發(fā)送,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Python實現(xiàn)迪杰斯特拉算法并生成最短路徑的示例代碼
這篇文章主要介紹了Python實現(xiàn)迪杰斯特拉算法并生成最短路徑的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12

