加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

django补充CBV和FBV模式

发布时间:2020-12-20 10:17:42 所属栏目:Python 来源:网络整理
导读:django 补充CBV 和FBV 模式 FBV 模式--- 函数: 经常用的方式 CBV 模式--- 类 CBV 中url 连接时函数名后面要接.as_view() class index (views.View ): @... def dispath(self,request,*arg,**kwarg): super... 如果对某一种请求做处理:单一装饰器 如果对
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,**kwargs)
return inner #闭包
class Login(views.View):
message = ‘‘
#分发器
def dispatch(self,**kwargs):
print("eric")
req = super(Login,self).dispatch(request,**kwargs)
print("ericc")
return req
@method_decorator(outer)
def get(self,**kwargs):
return render(request,"login.html")

@method_decorator(outer)
def post(self,**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})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读