def index(request): ''' request:django封裝的對(duì)象,它的類是WSGIRequest,它里面包含了所有http請(qǐng)求的東西 ''' 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,F(xiàn)IELS,body # 自定制請(qǐng)求頭 # 上傳文件使用的編碼方式是form-data,默認(rèn)編碼方式urlencoded print(request.is_ajax()) # 是不是ajax請(qǐng)求 print(request.path) # 請(qǐng)求路徑 print(request.get_full_path()) # 請(qǐng)求全路徑,帶數(shù)據(jù) # print(request.body) # 請(qǐng)求體,二進(jìn)制,如果傳文件,這個(gè)報(bào)錯(cuò) ''' 使用form表單,默認(rèn)情況下數(shù)據(jù)被轉(zhuǎn)成name=lqz&password=123放到請(qǐng)求體中 request.POST其實(shí)是從body中取出bytes格式的,轉(zhuǎn)成了字典 requet.GET其實(shí)是把路徑中?后面的部分拆出來(lái),轉(zhuǎn)成了字典 ''' print(request.encoding) # 客戶端向服務(wù)端傳遞時(shí),使用的編碼方法 print(request.META) # 重點(diǎn),字典,一堆東西,請(qǐng)求用戶的ip地址,請(qǐng)求頭中數(shù)據(jù),用戶自定制請(qǐng)求頭的數(shù)據(jù) ''' 把請(qǐng)求頭的key值部分統(tǒng)一加HTTP_ 并且全部轉(zhuǎn)成大寫(xiě) ''' print(request.META['REMOTE_ADDR']) # 客戶端的ip地址 print(request.FILES) # 客戶端上傳的文件 ########################3 暫時(shí)不用關(guān)注(后面會(huì)詳解) print(request.COOKIES) # 空字典 print(request.session) # session對(duì)象 print(request.user) # 匿名用戶 return HttpResponse('ok')
http請(qǐng)求頭,請(qǐng)求編碼格式3種
urlencoded form-data jason
### 重點(diǎn):JsonResponse的使用(看源碼) def index(request): # 三件套 # return HttpResponse('ok') # return render(request,'index.html',context={'name':'lili','age':18}) # return redirect('/home') # 重定向自己的地址,重定向第三方地址,經(jīng)常跟反向解析一起使用 # 向客戶端返回json格式數(shù)據(jù) # import json # res=json.dumps({'name':'張三','age':18},ensure_ascii=False) # return HttpResponse(res) # django內(nèi)置提供的JsonResponse # 本質(zhì)還是HttpResponse # ensure_ascii # return JsonResponse({'name':'張三','age':18},json_dumps_params={'ensure_ascii':False}) # safe,轉(zhuǎn)換除字典以外的格式,需要safe=False return JsonResponse([11,12,13,'lili',[1,2,3],{'name':'lili','age':19}],safe=False)
http響應(yīng)頭 編碼
content-type:text/html; charset=utf-8 # 返回?cái)?shù)據(jù)的編碼類型
容易混淆: enctype編碼格式(把數(shù)據(jù)以什么格式放到一起)不是字符編碼
CBV基于類的視圖(Class base view)和FBV基于函數(shù)的視圖(Function base view)
# 寫(xiě)視圖類(還是寫(xiě)在views.py中) ## 第一步,寫(xiě)一個(gè)類,繼承View from django.views import View class Index(View): def get(self, request): # 當(dāng)url匹配成功,get請(qǐng)求,會(huì)執(zhí)行它 return HttpResponse('ok') def post(self,request): return HttpResponse('post') ## 第二步:配置路由 path('index/', views.Index.as_view()), # 前期,全是FBV,后期,drf全是CBV
# 1 請(qǐng)求來(lái)了,路由匹配成功執(zhí)行 path('index/', views.Index.as_view()), 執(zhí)行views.Index.as_view()() # 2 本質(zhì)是執(zhí)行as_view()內(nèi)部有個(gè)閉包函數(shù)view() # 3 本質(zhì)是view()---》dispatch() # 4 dispatch內(nèi)部,根據(jù)請(qǐng)求的方法(get,post)---->執(zhí)行視圖類中的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="提交"></p> </form> # views.py def index(request): file=request.FILES.get('myfile') # 打開(kāi)一個(gè)空文件,寫(xiě)入 with open(file.name,'wb') as f: for line in file.chunks(): f.write(line) return HttpResponse('文件上傳成功')
聯(lián)系客服