• flask-蓝图


    蓝图作用

    相当于一个功能模块,能做到即插即用

    蓝图运用示例1:

    简单项目示例

     account.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from flask import Blueprint
    from flask import render_template
    from flask import request
    
    account = Blueprint('acc', __name__)
    
    
    @account.route('/login.html', methods=['GET', "POST"])
    def login():
        return render_template('login.html')

    blog.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from flask import Blueprint,url_for
    
    blog = Blueprint('blog', __name__)
    
    @blog.before_request
    def a():
        print("wo我是blog 的请求扩展")
    
    @blog.route("/index")
    def index():
        print(url_for("acc.login"))
        return "ok"

    user.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from flask import Blueprint
    
    user = Blueprint('user', __name__)

    __init__.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from flask import Flask
    
    app = Flask(__name__,template_folder='templates',static_folder='statics',static_url_path='/static')
    
    @app.before_request
    def a():
        print("我是app里面的befor_request")
    
    from .views.account import account
    from .views.blog import blog
    from .views.user import user
    
    app.register_blueprint(account)
    app.register_blueprint(blog)
    app.register_blueprint(user)

    run.py

    from pro_flask import app
    
    if __name__ == '__main__':
        app.run()

    复杂项目:

     admin/__init__.py

    from flask import Blueprint
    
    admin = Blueprint(
        'admin',
        __name__,
        template_folder='templates',
        static_folder='static'
    )
    from . import views
    View Code

    admin/views.py

    from . import admin
    
    
    @admin.route('/index')
    def index():
        return 'Admin.Index'
    View Code

    web/__init__.py

    from flask import Blueprint
    
    web = Blueprint(
        'web',
        __name__,
        template_folder='templates',
        static_folder='static'
    )
    from . import views
    View Code

    web/views.py

    from . import web
    
    @web.route('/index')
    def index():
        return 'Web.Index'
    View Code

    __init__.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    from flask import Flask
    from .admin import admin
    from .web import web
    
    app = Flask(__name__)
    app.debug = True
    
    app.register_blueprint(admin, url_prefix='/admin')
    app.register_blueprint(web)
    View Code

    run.py

    from pro_flask import app
    
    if __name__ == '__main__':
        app.run()
    View Code
  • 相关阅读:
    List转换为Datatable
    C# NPOI导出Excel和EPPlus导出Excel比较
    NPOI导出excel2007
    C#中的事务TransactionScope
    switch,case语句易误区
    消息队列mq总结
    一文搞定十大经典排序算法(Java实现)
    常见数据结构与算法整理总结(下)
    常见数据结构与算法整理总结(上)
    mysql开发常用技巧总结
  • 原文地址:https://www.cnblogs.com/xiaowangba9494/p/11863005.html
Copyright © 2020-2023  润新知