django 보충 CBV 및 FBV 모드

3196 단어
django  CBV FBV  
FBV --- :
CBV ---
CBV url .as_view()
class index(views.View):
@...
def dispath(self,request,*arg,**kwarg):
super...

:dispatch
:CBV method_decorator
from django.utils.decorators import method_decorator
get post view dispatch

from django import views
from django.utils.decorators import method_decorator
def outer(func):
def inner(request,*args,**kwargs):
print(request.method)
return func(request,*args,**kwargs)
return inner #
class Login(views.View):
message = ''
#
def dispatch(self, request, *args, **kwargs):
print("eric")
req = super(Login,self).dispatch(request,*args,**kwargs)
print("ericc")
return req
@method_decorator(outer)
def get(self,request,*args,**kwargs):
return render(request,"login.html")

@method_decorator(outer)
def post(self,request,*args,**kwargs):
user = request.POST.get("user")
pwd = request.POST.get("pwd")
c=1#
if c:
request.session["is_login"] = True
request.session["username"] = user
req=redirect("/login/")
return req
else:
message = " "
return render(request,"login.html",{"msg":message})

좋은 웹페이지 즐겨찾기