flask CBV

Basic Edition

from flask import Flask,views,url_for
 
app = Flask(__name__)
def tt(func):
   def inner(*args,**kwargs):
       print("你追到我。。")
       rv = func(*args,**kwargs)
       print("嘿嘿嘿。。。")
       return  rv
   return inner
 
class Index(views.View):
    methods = ["GET"] #规定哪些请求方式可以请求我这个路由
    decorators =[tt,]   #这个是给 我们的响应添加装饰器
    def dispatch_request(self):
        return "ojbk"
 
app.add_url_rule("/index",view_func=Index.as_view(name="index"),endpoint="index1")
# 为什么要给as_view传递name= "index",
#1 as_view再语法就要你传,
#2 他作用Index.as_view(name="index")他返回是的view这个函数对象,我们传递name="index"是给view的__name__改变名字。如果不传,我没有办法通过名字来找路由的映射关系,因为都是”view“

Common version

from flask import Flask,views,url_for
 
app = Flask(__name__)
def tt(func):
   def inner(*args,**kwargs):
       print("你追到我。。")
       rv = func(*args,**kwargs)
       print("嘿嘿嘿。。。")
       return  rv
   return inner
 
class Login(views.MethodView):
    methods = ["GET","POST"]  # 规定哪些请求方式可以请求我这个路由
    #decorators = [tt, ]  # 这个是给 我们的响应添加装饰器
    def get(self):
        print(url_for("index1"))
        return "get"
    def post(self):
        return "post"
 
app.add_url_rule("/login",view_func=Login.as_view(name="login"))
#实现方法是重写了dispatch_request,通过请求方法,来找到当前类中的函数。
 
if __name__ == '__main__':
    app.run()

There are 100 bottles of medicine, 1 bottle of poison, and now there are 7 mice. Now let you find out and take a bottle of poison in 1 day. The rats will not die immediately after drinking the medicine. They will die the next day. What do you do?

2**7=128,---》2**6 = 64 
 
100瓶,编号1-100  10
7个老鼠,
     a,  b, c,  d, e, f  g
1    0   0  0    0  0 0  1
2    0   0  0    0  0  1  0
3    0   0  0    0  0  1 1
4    0   0   0   0   1 0 0
5    0  -0  0   0   1 0  1
10   0  0   0   1   0 1  0
f,g说明3瓶药有毒

Guess you like

Origin www.cnblogs.com/cnhyk/p/12758393.html