需求:前端
解決:正則表達式
代碼參考:django
①未命名參數(位置參數):按定義順序傳遞json
# 在項目下urls.py文件中配置 url(r'^news/(\d+)/(\d+)$', users.views.news), #在users.views.py中定義 def news(request, a, b): return HttpResponse('顯示新聞:{} {}'.format(a, b))
②命名參數(關鍵字參數):按定義的組名傳遞安全
# 在項目下的urls.py文件中配置 url(r'^news/(?P<category>\d+)/(?P<page>\d+)$', users.views.news), #在users.views.py文件中定義 def news(request, category, page): return HttpResponse('顯示新聞:{} {}'.format(category, page))
需求:服務器
獲取URL地址 http://127.0.0.1:8000/news?categeory=1&page=2中的查詢字符串的值app
代碼參考:ide
# url配置 url(r'^news/$', users.views.news), # 視圖函數 def news(request): category = request.GET.get('category') page = request.GET.get('page') text = '獲取查詢字符串:<br/> category={}, page={}'.format(category, page) return HttpResponse(text)
重要:查詢字符串不卻分請求方式,即便客戶端經過POST方式發起請求,依然能夠經過request.GET獲取請求中的查詢字符串數據函數
請求體數據格式不固定,能夠是表單類型字符串,能夠是JSON字符串,能夠是XML字符串,應區別對待。測試
能夠發送的請求體數據的 請求方式有POST、PUT、PATCH、DELETE
Django對POST、PUT、PATCH、DELETE請求方式開啓了CSRF安全防禦,爲了方便測試,能夠在settings.py文件中註釋掉CSRF中間件,關閉CSRF防禦
獲取表單數據 Form Data()鍵值對
前端發送的表單或鍵值對類型的請求體數據,能夠經過request.POST屬性獲取
# url配置 url(r'^news/$', users.views.news), # 視圖函數 def news(request): category = request.POST.get('category') page = request.POST.get('page') text = '獲取body中的鍵值對:<br/> category={}, page={}'.format(category, page) return HttpResponse(text)
使用Postman發送表單數據
重要:request.POST只能用來獲取POST方式的請求體表單數據或鍵值對數據。若是爲非請求提交的請求體數據,或者是請求體數據類型爲非表單或非鍵值對數據,則須要經過request.body屬性獲取提交的數據後本身解析
非表單類型None-Form Data
例如: 獲取請求體中的json數據 {"category": 1, "page": 2}
# url配置 url(r'^news/$', users.views.news), # 視圖函數 def news(request): # 獲取JSON字符串 json_str = request.body #解析json dict_data = json.loads((json_str)) category = dict_data.get('category') page = dict_data.get('page') text = '獲取body中的json數據:<br/> category={}, page={}'.format(category, page) return HttpResponse(text)
能夠經過request.META屬性獲取請求頭headers中的數據,request.META爲字典類型
CONTENT_LENGTH – The length of the request body (as a string). CONTENT_TYPE – The MIME type of the request body. HTTP_ACCEPT – Acceptable content types for the response. HTTP_ACCEPT_ENCODING – Acceptable encodings for the response. HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response. HTTP_HOST – The HTTP Host header sent by the client. HTTP_REFERER – The referring page, if any. HTTP_USER_AGENT – The client’s user-agent string. QUERY_STRING – The query string, as a single (unparsed) string. REMOTE_ADDR – The IP address of the client. REMOTE_HOST – The hostname of the client. REMOTE_USER – The user authenticated by the Web server, if any. REQUEST_METHOD – A string such as "GET" or "POST". SERVER_NAME – The hostname of the server. SERVER_PORT – The port of the server (as a string).
注意: 獲取自定義的請求頭屬性值時,須要添加前綴HTTP_並轉換成大寫,做爲鍵來獲取值
# url配置 url(r'^news/$', users.views.news), # 視圖函數 def news(request): a = request.META.get('HTTP_A') b = request.META.get('HTTP_B') text = '獲取headers的數據:<br/> a={}, b={}'.format(a, b) return HttpResponse(text)
視圖必須返回一個HttpResponse對象(或其子類), 能夠將要返回的字符串數據傳給HttpResponse對象再返回
HttpRequst對象由Django建立,HttpResponse對象由開發人員建立
HttpResponse的經常使用子類
HttpResponse
能夠使用django.http.HttpResponse來構造響應對象
def news(request): # HttpResponse(content=響應體, content_type=響應體數據MIME類型, status=狀態碼) json_str = '{"name": "heboan"}' return HttpResponse(json_str, content_type='application/json', status=400)
響應頭設置:能夠直接將HttpResponse對象當作字典進行響應頭鍵值對的設置
def news(request): # HttpResponse(content=響應體, content_type=響應體數據MIME類型, status=狀態碼) response = HttpResponse('my name is heboan') response['name'] = 'heboan' return response
JsonResponse對象
幫助咱們將數據轉換爲json字符串,再返回給客戶端,會自動設置響應頭Conten-Type爲application/json
from django.http import HttpResponse, JsonResponse .. def news(request): return JsonResponse({'name':'heboan'})
JsonResponse能夠接收非字典數據,須要指定safe=False
redirect重定向
重定向:不返回具體顯示內容給客戶端,讓客戶端從新請求返回的地址,獲取內容
from django.shortcuts import redirect ... def news(request): return redirect('/index')