Flask请求钩子与app各组件初始化

通过装饰器为一个模块添加请求钩子, 对当前模块的请求进行额外的处理. 比如权限验证,请求钩子放在app文件加的 __init__.py  create_app函数中

def create_app(config_name):
    app = Flask(__name__)

    # config  Debug = True
    app.config.from_object(config_name)

    from modules import blue_index
    from modules import blue_user
    from modules import blue_admin

    app.register_blueprint(blue_index)
    app.register_blueprint(blue_user)
    app.register_blueprint(blue_admin)

    # 钩子函数 before_first_request
    @app.before_first_request
    def before_first():
        print("app.before_first")

    # 钩子函数 before_request
    @app.before_request
    def before():
        print("app.before")

    # 钩子函数 after_request
    @app.after_request
    def after(response):
        print("app.after")
        return response

    # 钩子函数 teardown_request
    @app.teardown_request
    def teardown(e):
        print("app.teardown")

    return app

创建app与各组件初始化

from flask import Flask,  render_template
from config import config
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_login import LoginManager
from flask_pagedown import PageDown
from flask_moment import Moment
import datetime
from flask_wtf.csrf import CsrfProtect



""" 各组件初始化 """
#开启csrf防护
csrf = CsrfProtect()
bootstrap = Bootstrap()
db = SQLAlchemy()
mail = Mail()
login_manager = LoginManager()
# session安全保护级别设置
login_manager.session_protection = 'basic'
# 默认登录入口
login_manager.login_view = 'auth.login'
pagedown = PageDown()
moment = Moment()


""" 根据配置选项创建Flask APP """
def create_app(config_name):
    app = Flask(__name__)
    # 使用chartkick配合falsk画报表
    app.jinja_env.add_extension("chartkick.ext.charts")
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    csrf.init_app(app)
    bootstrap.init_app(app)
    db.init_app(app)
    mail.init_app(app)
    login_manager.init_app(app)
    pagedown.init_app(app)
    moment.init_app(app)

    """ 按照功能模块来组织蓝图 """
    from .index import index as index_blueprint
    app.register_blueprint(index_blueprint, url_prefix='/')

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint, url_prefix='/srcpm')

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/srcpm/auth')

    from .admin import admin as admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/srcpm/admin')

    from .src import src as src_blueprint
    app.register_blueprint(src_blueprint, url_prefix='/srcpm/src')

    from .drops import drops as drops_blueprint
    app.register_blueprint(drops_blueprint, url_prefix='/srcpm/drops')

    return app

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值