四 .Flask 模板 中间件 特殊装饰器 基础知识(使用)

一 Flask 模板 中间件 特殊装饰器

1 .Flask 模板语法直接看面效果

https://www.cnblogs.com/lovershowtime/p/11349576.html    模板  和Django模板类似

edit.html

<form> asdfasdf asdfasdf asdf asdf 哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 </form>
login.html

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h1>模板11111111</h1> {% block content %} {% endblock %} </body> </html>
show.html

{% extends "login.html"%} {# 引入继承模板 语法#} {% block content %} {# 继承模板 语法#} {{users.0}} {{users[0]}} {{txt}} <!--{{txt|safe}}--> {{func(6)}} {# 单个函数使用#} {{sb(1,9)}} {# 这个装饰器 是全局装饰器直接调用 传参数#} {{ 1|db(2,3) }} {#@app.template_filter() 使用这个装饰器 必须这样传参数#} {% if 1|db(2,3) %} <div>333</div> {% else %} <div>999</div> {% endif %} {% include "edit.html" %} {#引入其他模板#} {# 这Flask 里面的宏定义(相当一个函数)#} {% macro ccccc(name, type='text', value='') %} <h1>哈哈哈哈</h1> <input type="{{ type }}" name="{{ name }}" value="{{ value }}"> <input type="submit" value="提交"> {% endmacro %} {{ ccccc('n1') }} {# 这里相当于调用#} {{ ccccc('n2') }} {# 这里相当于调用#} {{ ccccc('n3') }} {# 这里相当于调用#} {% endblock %}
app.py

from
flask import Flask, request,jsonify,json,render_template,redirect,url_for,session,Markup,flash,get_flashed_messages app=Flask(__name__) # 全局定义函数 @app.template_global() # 这个装饰器 装饰的函数可以全局使用 可以不用传参到模板中 而模板中可以直接使用或者调用 def sb(a1, a2): # {{sb(1,9)}} return a1 + a2 # 全局定义函数 @app.template_filter() # 这个装饰器过滤 装饰的函数可以全局使用 可以不用传参到模板中 而模板中可以直接使用或者调用 def db(a1, a2, a3): # {{ 1|db(2,3) }} 传参方式 return a1 + a2 + a3 def func(arg): # 单个函数需要传参数到模板 使用 return arg + 1 @app.route('/tpl/') def tpl(): context = { 'users':['李四','展示给乌鸡','啦啦啦啦'], 'txt':Markup("<input type='text' />"), 'func':func } return render_template('show.html',**context) if __name__=="__main__": app.run()



2 .Flask 中间件

https://www.cnblogs.com/lovershowtime/p/11384508.html     django 中间件 但是和Flask中间件不一样

- call方法什么时候出发?
            - 用户发起请求时,才执行。
        - 任务:在执行call方法之前,做一个操作,call方法执行之后做一个操作。
from flask import Flask
app = Flask(__name__)
@app.route('/index')
def index():
    print('index')
    return "Index"

class Middleware(object):
    def __init__(self,old):
        self.old = old
    def __call__(self, *args, **kwargs):
        ret = self.old(*args, **kwargs)
        return ret

if __name__ == '__main__':
    app.wsgi_app = Middleware(app.wsgi_app)
    app.run()

 3. 特殊装饰器(有点向django中间)

      @app.template_global                 在模板中使用  定义全局使用的装饰器 (使用方法请看上面模板)
       
     @app.template_filter                  在模板中使用  定义全局使用的装饰器
 
 
 

@app.before_request  重点

@app.after_request   重点

before_request 没有返回 值的情况下 看图

from
flask import Flask app = Flask(__name__) @app.before_request def aa(): print('aa1') @app.after_request def bb(cc): print('bb22') return cc @app.route("/index/") def index(): print("index") return "index1111" @app.route("/home/") def home(): print("home") return "home22222222" if __name__ == '__main__': app.run()


 
  
before_request 有 返回值的情况下 看图
from flask import Flask
app = Flask(__name__)
@app.before_request
def aa():
    print('aa1')
    return "哈哈哈哈1111"

@app.after_request
def bb(cc):
    print('bb22')
    return cc

@app.route("/index/")
def index():
      print("index")

      return "index1111"
@app.route("/home/")
def home():
      print("home")
      return "home22222222"
if __name__ == '__main__':
    app.run()

from flask import Flask
app = Flask(__name__)
@app.before_first_request   不常用 了解
def x1():
    print('123123')

@app.route('/index/')
def index():
    print('index')
    return "Index"

@app.route('/home/')
def order():
    print('order')
    return "order"

@app.errorhandler(404)         不常用 了解
def not_found(arg):
print(arg) return "没找到"

if __name__ == '__main__':
app.run()


猜你喜欢

转载自www.cnblogs.com/lovershowtime/p/11735954.html