一、視圖層之請求物件
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')
http請求頭,請求編碼格式3種
urlencoded form-data jason
二、視圖層之回應物件
### 重點:JsonResponse的使用(看原始碼) def index(request): # 三件套 # return HttpResponse('ok') # return render(request,'index.html',context={'name':'lili','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,'lili',[1,2,3],{'name':'lili','age':19}],safe=False)
http回應頭 編碼
content-type:text/html; charset=utf-8 # 回傳資料的編碼型別
容易混淆: enctype編碼格式(把資料以什么格式放到一起)不是字符編碼
三、cbv和fbv
CBV基于類的視圖(Class base view)和FBV基于函式的視圖(Function base view)
# 寫視圖類(還是寫在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
四、cbv本質

# 1 請求來了,路由匹配成功執行 path('index/', views.Index.as_view()), 執行views.Index.as_view()() # 2 本質是執行as_view()內部有個閉包函式view() # 3 本質是view()---》dispatch() # 4 dispatch內部,根據請求的方法(get,post)---->執行視圖類中的def get def post
五、簡單檔案上傳
# 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=https://www.cnblogs.com/guojieying/archive/2020/10/09/"提交"></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('檔案上傳成功')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/166829.html
標籤:其他
上一篇:秦時明月更新了?青春回來了
下一篇:Django——模版層(前后端互動編碼方式,django模板使用的兩種方式,模板語法之變數&深度查詢句點符,模板渲染成標簽還是原字串,過濾器,標簽)
