FBV:function base viewdjango
url(r'^index$',user)
def users(request): return HttpResponse(‘OK’)
CBV:class base viewjson
url(r'^students/', views.StudentsView.as_view())
from django.views import View class StudentsView(View): def get(self,request,*args,**kwargs): return HttpResponse('GET') def post(self, request, *args, **kwargs): return HttpResponse('POST') def put(self, request, *args, **kwargs): return HttpResponse('PUT') def delete(self, request, *args, **kwargs): return HttpResponse('DELETE')
CBV實現原理瀏覽器
基於反射實現根據請求方式不一樣,執行不一樣的方法。具體實現流程:url中的as_view() —》 view方法 —》 dispatch方法(根據反射執行相應函數)session
CBV中可使用的方法:app
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
from django.views import View class RestApi(View): def dispatch(self,request,*args,**kwargs): func = getattr(self,request.method.lower()) ret = func(request,*args,**kwargs) return ret def get(self,request,*args,**kwargs): return HttpResponse('get') def post(self,request,*args,**kwargs): return HttpResponse('post') def put(self,request,*args,**kwargs): return HttpResponse('put') def delete(self,request,*args,**kwargs): return HttpResponse('delete')
from django.views import View class RestApi(View): def dispatch(self,request,*args,**kwargs): ret = super(RestApi, self).dispatch(request,*args,**kwargs) return ret def get(self,request,*args,**kwargs): return HttpResponse('get') def post(self,request,*args,**kwargs): return HttpResponse('post') def put(self,request,*args,**kwargs): return HttpResponse('put') def delete(self,request,*args,**kwargs): return HttpResponse('delete')
django 中的中間件(middleware),在django中,中間件其實就是一個類,在請求到來和結束後,django會根據本身的規則在合適的時機執行中間件中相應的方法。ide
咱們從瀏覽器發出一個請求 Request,獲得一個響應後的內容 HttpResponse ,這個請求傳遞到 Django的過程以下:函數
每個請求都是先經過中間件中的 process_request 函數,這個函數返回 None 或者 HttpResponse 對象,若是返回前者,繼續處理其它中間件,若是返回一個 HttpResponse,就處理停止,返回到網頁上。post
中間件中能夠定義4個方法,分別是:url
執行順序也是按照以上順序執行的.spa
自定義中間件
from django.utils.deprecation import MiddlewareMixin class test1(MiddlewareMixin): def process_request(self, request): print('wusir') class test2(MiddlewareMixin): def process_request(self, request): print('jack')
註冊中間件
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'wusir.m1.test1', 'wusir.m1.test2', ]
終端輸出結果
wusir jack tom [17/Dec/2018 17:08:04] "GET /app/aa HTTP/1.1" 200 2
PS:與mange.py在同一目錄下的文件夾 wusir下的m1.py文件中的test1,test2類
中間件能夠作什麼
狀況一: MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', # 全站使用csrf認證 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] from django.views.decorators.csrf import csrf_exempt @csrf_exempt # 該函數無需認證 def users(request): user_list = ['alex','oldboy'] return HttpResponse(json.dumps((user_list))) 狀況二: MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', # 全站不使用csrf認證 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] from django.views.decorators.csrf import csrf_exempt @csrf_protect # 該函數需認證 def users(request): user_list = ['alex','oldboy'] return HttpResponse(json.dumps((user_list)))
CSRF在CBV中的使用補充
裝飾器@csrf_exempt,@csrf_protect必須在dispatch方法中(單獨方法無效)
方式一: from django.views.decorators.csrf import csrf_exempt,csrf_protect from django.utils.decorators import method_decorator class StudentsView(View): @method_decorator(csrf_exempt) def dispatch(self, request, *args, **kwargs): return super(StudentsView,self).dispatch(request, *args, **kwargs) def get(self,request,*args,**kwargs): print('get方法') return HttpResponse('GET') def post(self, request, *args, **kwargs): return HttpResponse('POST') def put(self, request, *args, **kwargs): return HttpResponse('PUT') def delete(self, request, *args, **kwargs): return HttpResponse('DELETE') 方式二: from django.views.decorators.csrf import csrf_exempt,csrf_protect from django.utils.decorators import method_decorator @method_decorator(csrf_exempt,name='dispatch') class StudentsView(View): def get(self,request,*args,**kwargs): print('get方法') return HttpResponse('GET') def post(self, request, *args, **kwargs): return HttpResponse('POST') def put(self, request, *args, **kwargs): return HttpResponse('PUT') def delete(self, request, *args, **kwargs): return HttpResponse('DELETE')