FBV mode decorator:
Ordinary function decorator (syntactic sugar!)
views.py
1 from django.shortcuts import render 2 3 def wrapper(f): 4 def inner(*args,**kwargs): 5 print("before") 6 ret=f(*args,**kwargs) 7 print("after") 8 return ret 9 return inner 10 11 @wrapper 12 def index(request): 13 return render(request,"index.html")
CBV mode decorator:
CBV must be imported in view mode function: from django.views Import View
(1) to rewrite the parent dispatch distribution method, the distribution function before and after the execution of each request response function is implemented with the corresponding analog decorator
views.py
1 from django.shortcuts import render,HttpResponse 2 from django.views import View 3 from django.utils.decorators import method_decorator 4 5 class Myview(View): 6 7 def dispatch(self, request, *args, **kwargs): 8 print("before") 9 ret=super().dispatch(request, *args, **kwargs) 10 print("after") 11 return ret 12 13 def get(self, request): 14 return render(request, "login.html") 15 16 def post(self, request): 17 if request.method == "GET": 18 return render(request, "login.html") 19 elif request.method == "POST": 20 if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "123": 21 return render(request, "login_success.html", {"name": request.POST.get("username")}) 22 else: 23 return HttpResponse("账号或密码有误!")
( 2 ) when the decorator plus a subclass overrides distribution function (function for each request will be decorated)
You must be imported:
from django.views import View
from django.utils.decorators import method_decorator
views.py
1 from django.shortcuts import render,HttpResponse 2 from django.views import View 3 from django.utils.decorators import method_decorator 4 5 def wrapper(f): 6 def inner(*args,**kwargs): 7 print("before") 8 ret=f(*args,**kwargs) 9 print("after") 10 return ret 11 return inner 12 13 class Myview(View): 14 15 @method_decorator(wrapper) 16 def dispatch(self, request, *args, **kwargs): 17 ret=super().dispatch(request, *args, **kwargs) 18 return ret 19 20 def get(self, request): 21 return render(request, "login.html") 22 23 def post(self, request): 24 if request.method == "GET": 25 return render(request, "login.html") 26 elif request.method == "POST": 27 if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "123": 28 return render(request, "login_success.html", {"name": request.POST.get("username" )}) 29 the else : 30 return HttpResponse ( " account number or password is incorrect! " )
( 3 ) in response to different subclasses override requests function plus the decorator
You must be imported:
from django.views import View
from django.utils.decorators import method_decorator
views.py
1 from django.shortcuts import render,HttpResponse 2 from django.views import View 3 from django.utils.decorators import method_decorator 4 5 def wrapper(f): 6 def inner(*args,**kwargs): 7 print("before") 8 ret=f(*args,**kwargs) 9 print("after") 10 return ret 11 return inner 12 13 class Myview(View): 14 # def dispatch(self, request, *args, **kwargs): 15 # ret=super().dispatch(request, *args, **kwargs) 16 # return ret 17 18 def get(self, request): 19 return render(request, "login.html") 20 21 @method_decorator(wrapper) 22 def post(self, request): 23 if request.method == "GET": 24 return render(request, "login.html") 25 elif request.method == "POST": 26 if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "123": 27 return render(request, "login_success.html", {"name": request.POST.get("username")}) 28 the else : 29 return HttpResponse ( " account number or password is incorrect! " )
( 4 ) adding at decorators subclass definition, must be specified and the only specified function added
You must be imported:
from django.views import View
from django.utils.decorators import method_decorator
views.py
1 from django.shortcuts import render,HttpResponse 2 from django.views import View 3 from django.utils.decorators import method_decorator 4 5 6 def wrapper(f): 7 def inner(*args,**kwargs): 8 print("before") 9 ret=f(*args,**kwargs) 10 print("after") 11 return ret 12 return inner 13 14 @method_decorator(wrapper,name="post") 15 16 class Myview(View): 17 # def dispatch(self, request, *args, **kwargs): 18 # ret=super().dispatch(request, *args, **kwargs) 19 # return ret 20 21 def get(self, request): 22 return render(request, "login.html") 23 24 def post(self, request): 25 if request.method == "GET": 26 return render(request, "login.html") 27 elif request.method == "POST": 28 if request.POST.get("username") == "yang" and request.POST.get("userpsd") == "123": 29 return render(request, "login_success.html", {"name": request.POST.get(" Username " )}) 30 the else : 31 return HttpResponse ( " account number or password is incorrect! " )
Other decorators:
· Added before the decorator must be imported from django.utils.decorators import method_decorator
· Add a decorator's format must be @method_decorator () , brackets function name for the decorator
· To add a class must declare name
* Note csrf-token decorator peculiarities in CBV mode, it can only be added to the dispatch above ( later to say )
Here it is csrf_token decorators:
@csrf_protect , forced to current function CSRF prevention function, even if the settings are not provided csrfToken global middleware.
@csrf_exempt , cancels the current function CSRF prevention function, even if the settings set in the global middleware.
注意:from django.views.decorators.csrf import csrf_exempt,csrf_protect