瀏覽器和伺服器之間資料讀取
阿新 • • 發佈:2018-12-04
瀏覽器傳送的資料格式:
get: 是從url欄傳送 例如:/index/?a=1&b=2
get方式沒有請求體 request.body 是一個空的 b''
post: 通過form表單傳送 預設傳送的資料型別是urlencoded
只有post才有請求體
<form action="" method="post"> {%csrf_token%} <input type="text" name="abc"> <input type="submit"> 或者 用button按鈕 </form>
form表單是以name為鍵, 以使用者輸入的內容為值, 以鍵值對的形式發給伺服器的
伺服器接收的資料格式:
get請求:
request.GET: <QueryDict: {'a': ['1'], 'b': ['2']}>
request.body: b''
post請求:
request.POST: <QueryDict: {'csrfmiddlewaretoken': ['Cq3vrx44y1NGgAgUupJ1eBP4NBbGaLWwq5Cx43acmB4AYRIiqYh2LB0wsGJTjJwq'], 'abc': ['222222222']}>
request.body: b'csrfmiddlewaretoken=Cq3vrx44y1NGgAgUupJ1eBP4NBbGaLWwq5Cx43acmB4AYRIiqYh2LB0wsGJTjJwq&abc=222222222'
從伺服器中取資料:
get 形式:
伺服器接收到的資料 <QueryDict: {'a': ['1'], 'b': ['2']}>
recv= request.GET.get(“a”) #get取資料 print(recv) #1
post形式:
伺服器接收的資料形式:
<QueryDict: {'csrfmiddlewaretoken': ['rXl2iAESA6Cju8y24Zc4oz3hDUIkwqnbfCU4V6K0oGTdcp0q0yK5VzeJiZgxFoX5'], 'abc': ['111111111111111']}>
print(request.POST.get("abc")) #post取資料