亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

django中使用POST方法獲取POST數(shù)據(jù)

 更新時(shí)間:2019年08月20日 16:56:44   作者:我愛(ài)學(xué)python  
這篇文章主要介紹了django中使用POST方法獲取POST數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在django中獲取post數(shù)據(jù),首先要規(guī)定post發(fā)送的數(shù)據(jù)類型是什么。

1.獲取POST中表單鍵值數(shù)據(jù)

如果要在django的POST方法中獲取表單數(shù)據(jù),則在客戶端使用JavaScript發(fā)送POST數(shù)據(jù)前,定義post請(qǐng)求頭中的請(qǐng)求數(shù)據(jù)類型:

xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

在django的views.py相關(guān)方法中,需要通過(guò)request.POST獲取表單的鍵值數(shù)據(jù),并且可以通過(guò)reques.body獲取整個(gè)表單數(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)

相關(guān)日志:

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請(qǐng)求頭中設(shè)置請(qǐng)求數(shù)據(jù)類型:

xmlhttp.setRequestHeader("Content-type","application/json");

在django的views.py中導(dǎo)入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)

相關(guān)日志:

the POST method
<QueryDict: {}>
<type 'str'>
{"sdf":23}
{u'sdf': 23}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論