1. 程式人生 > 實用技巧 >django之檢視層等相關內容-64

django之檢視層等相關內容-64

1 檢視層之請求物件

def index(request):
'''
request:django封裝的物件,它的類是WSGIRequest,它裡面包含了所有http請求的東西
'''
print(request)
print(type(request))
# from django.core.handlers.wsgi import WSGIRequest
#######################1 講過的
print(request.method)
print(request.GET)
print(request.POST)

########################2 新講的path,get_full_path,META,FIELS,body
# 自定製請求頭
# 上傳檔案使用的編碼方式是form-data,預設編碼方式urlencoded
print(request.is_ajax()) # 是不是ajax請求
print(request.path) # 請求路徑
print(request.get_full_path()) # 請求全路徑,帶資料

# print(request.body) # 請求體,二進位制,如果傳檔案,這個報錯
'''
使用form表單,預設情況下資料被轉成name=lqz&password=123放到請求體中
request.POST其實是從body中取出bytes格式的,轉成了字典
requet.GET其實是把路徑中?後面的部分拆出來,轉成了字典
'''
print(request.encoding) # 客戶端向服務端傳遞時,使用的編碼方法

print(request.META) # 重點,字典,一堆東西,請求使用者的ip地址,請求頭中資料,使用者自定製請求頭的資料
'''
把請求頭的key值部分統一加HTTP_ 並且全部轉成大寫
'''
print(request.META['REMOTE_ADDR']) # 客戶端的ip地址
print(request.FILES) # 客戶端上傳的檔案



########################3 暫時不用關注(後面會講)
print(request.COOKIES) # 空字典
print(request.session) # session物件
print(request.user) # 匿名使用者
return HttpResponse('ok')

2 檢視層之響應物件

### 重點:JsonResponse的使用(看原始碼)

def index(request):
# 三件套
# return HttpResponse('ok')
# return render(request,'index.html',context={'name':'lqz','age':18})
# return redirect('/home') # 重定向自己的地址,重定向第三方地址,經常跟反向解析一起使用

# 向客戶端返回json格式資料
# import json
# res=json.dumps({'name':'劉清政','age':18},ensure_ascii=False)
# return HttpResponse(res)
# django內建提供的JsonResponse
# 本質還是HttpResponse

# ensure_ascii
# return JsonResponse({'name':'劉清政','age':18},json_dumps_params={'ensure_ascii':False})
# safe,轉換除字典以外的格式,需要safe=False
return JsonResponse([11,12,13,'lqz',[1,2,3],{'name':'lqz','age':19}],safe=False)

3 cbv和fbv

# CBV基於類的檢視(Class base view)和FBV基於函式的檢視(Function base view)
# 之前學的全是FBV,寫的是檢視函式

# 寫檢視類(還是寫在views.py中)
## 第一步,寫一個類,繼承View
from django.views import View

class Index(View):
def get(self, request): # 當url匹配成功,get請求,會執行它
return HttpResponse('ok')

def post(self,request):
return HttpResponse('post')

## 第二步:配置路由
path('index/', views.Index.as_view()),


# 前期,全是FBV,後期,drf全是CBV



4 檔案上傳

## html注意編碼方式
<form action="/index/" method="post" enctype="multipart/form-data">

<p>使用者名稱:<input type="text" name="name"></p>
<p>密碼:<input type="password" name="password"></p>
<p><input type="file" name="myfile"></p>
<p><input type="submit" value="提交"></p>
</form>

# views.py
def index(request):
file=request.FILES.get('myfile')
# 開啟一個空檔案,寫入
with open(file.name,'wb') as f:
for line in file.chunks():
f.write(line)
return HttpResponse('檔案上傳成功')

5 postman軟體

模擬傳送http請求(控制請求路徑,請求方式,請求頭,請求體)

6 form表單,提交地址

# action
#1 不寫,預設向當前地址傳送請求
#2 /index/,向當前域(http://127.0.0.1:8000/)的/index/傳送請求
#3 http://127.0.0.1:8000/index/,向該地址傳送請求(可以向第三方服務傳送請求)

# method
# 1 post:傳送post請求(預設編碼情況下:以key=value&key=value的形式拼到請求體中)
# 2 get:傳送get請求(以key=value&key=value的形式拼到路徑中)
<form action="/index/" method="post">

<p>使用者名稱:<input type="text" name="name"></p>
<p>密碼:<input type="text" name="password"></p>
<p><input type="submit" value="提交"></p>
</form>

7 Pycharm的自動提示

from django.core.handlers.wsgi import WSGIRequest
# pycharm的自動提示
request=request # type: WSGIRequest