• Flask 基础组件(九):请求扩展


    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from flask import Flask, Request, render_template
    
    app = Flask(__name__, template_folder='templates')
    app.debug = True
    
    
    @app.before_first_request
    def before_first_request1():
        print('before_first_request1')
    
    
    @app.before_first_request
    def before_first_request2():
        print('before_first_request2')
    
    
    @app.before_request
    def before_request1():
        Request.nnn = 123
        print('before_request1')
    
    
    @app.before_request
    def before_request2():
        print('before_request2')
    
    
    @app.after_request
    def after_request1(response):
        print('before_request1', response)
        return response
    
    
    @app.after_request
    def after_request2(response):
        print('before_request2', response)
        return response
    
    
    @app.errorhandler(404)
    def page_not_found(error):
        return 'This page does not exist', 404
    
    
    @app.template_global()
    def sb(a1, a2):
        return a1 + a2
    
    
    @app.template_filter()
    def db(a1, a2, a3):
        return a1 + a2 + a3
    
    
    @app.route('/')
    def hello_world():
        return render_template('hello.html')
    
    
    if __name__ == '__main__':
        app.run()
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from flask import Flask, Request, render_template
    
    app = Flask(__name__, template_folder='templates')
    app.debug = True
    
    
    @app.before_first_request
    def before_first_request1():
        print('before_first_request1')
    
    
    @app.before_first_request
    def before_first_request2():
        print('before_first_request2')
    
    
    @app.before_request
    def before_request1():
        Request.nnn = 123
        print('before_request1')
    
    
    @app.before_request
    def before_request2():
        print('before_request2')
    
    
    @app.after_request
    def after_request1(response):
        print('before_request1', response)
        return response
    
    
    @app.after_request
    def after_request2(response):
        print('before_request2', response)
        return response
    
    
    @app.errorhandler(404)
    def page_not_found(error):
        return 'This page does not exist', 404
    
    
    @app.template_global()
    def sb(a1, a2):
        return a1 + a2
    
    
    @app.template_filter()
    def db(a1, a2, a3):
        return a1 + a2 + a3
    
    
    @app.route('/')
    def hello_world():
        return render_template('hello.html')
    
    
    if __name__ == '__main__':
        app.run()

     

  • 相关阅读:
    Django contenttypes组件
    Django admin组件使用
    Django 信号
    Django 中间件
    Django 分页组件替换自定义分页
    Django ORM操作补充
    Django ORM 操作2 增删改
    Django 序列化
    JavaScript
    CSS 属性
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/12814306.html
Copyright © 2020-2023  润新知