zoukankan      html  css  js  c++  java
  • flask之蓝图

    一:蓝图的作用

    将视图函数按照功能进行划分,让不同的模块的视图函数,在不同的文件进行管理,也方便大家进行开发,要不然大家都给同时修改app.py文件。

    不进行划分的样子

    app.py

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/index")
    def index():
        return "index_page"
    
    @app.route("/login")
    def login():
        return "login_page"
    
    @app.route("/register")
    def resigter():
        return "register_page"
    
    ...........................
    # 一个项目会有多个功能,完整的写完,这里面需要写很多个视图,管理起来非常麻烦
    
    if __name__ == "__main__":
        app.run(debug=True)    

    二:蓝图的使用

    (1)创建蓝图对象

    from flask import Blueprint
    user = Blueprint("user蓝图的别名"__name__# "user蓝图的别名",在url_for的时候会使用到。

    (2)蓝图对象装饰视图函数

    @user.route("/login")
    def login():
        return "login_page"

    (3)对蓝图进行注释

    # 为了让 url_map识别蓝图,注册后会在url_map看到视图函数和路径的对应关系
    from xxxx import user
    app.resigter_blueprint(user)

    app.py

    from flask import Flask
    from blue_print_sutudy.blueprint_config import user_module_blueprint
    app = Flask(__name__)
    print(app.url_map)
    app.register_blueprint(user_module_blueprint)
    print(app.url_map)
    if __name__ == '__main__':
        app.run(debug=True,host="0.0.0.0",port=8888)
    
    
    # 结果
    注册前:Map([<Rule '/static/<filename>' (HEAD, GET, OPTIONS) -> static>]) 
    注册后:Map([<Rule '/user/login' (HEAD, GET, OPTIONS) -> user_module_blueprint.login>,
     <Rule '/static/<filename>' (HEAD, GET, OPTIONS) -> static>])

    blueprint_config.py

    from flask import Blueprint
    user_module_blueprint = Blueprint("user_module_blueprint",__name__)
    
    
    @user_module_blueprint.route("/user/login")
    def login():
        return "login_page"

    三:蓝图的在项目中的使用

    goods_module.__init__.py

    from flask import Blueprint
    goods_module_blueprint = Blueprint("goods_module_blueprint",__name__)
    from .view import * # 作用是为了进入view里,从上到下执行一遍代码

    goods_module.view.py

    from . import goods_module_blueprint
    
    @goods_module_blueprint.route("/goods/img")
    def goods_img():
        return "goods_img"
    
    @goods_module_blueprint.route("/goods/price")
    def goods_price():
        return "goods_price"

    app.py

    from flask import Flask
    # 导包,其实就是去找__init__.py执行里面的代码,因此__all__=[名字1,名字2,名字3],就是能够识别这个包下的所有文件
    from blue_print_sutudy.goods_module import goods_module_blueprint from blue_print_sutudy.user_module import user_module_blueprint app = Flask(__name__) app.register_blueprint(user_module_blueprint) app.register_blueprint(goods_module_blueprint) if __name__ == '__main__': app.run(debug=True,host="0.0.0.0",port=8888)

    # TODO

  • 相关阅读:
    Mysql使用指南
    数据库中的脏读、幻读、不可重复读
    数据库分库分表策略
    php匹配html中的日期进行修改并且重新写入html
    程序猿,你也配吃10元的盒饭?
    git excutable file not found in %path%
    html2canvas+jspdf 完美解决html导出且分页 解决图片显示不全问题
    laravel+gatewayworker+layer搭建网页聊天系统1--workerman安装
    ubuntu使用querylist+cron实现每日新闻采集
    Command "make:console" is not defined.
  • 原文地址:https://www.cnblogs.com/meloncodezhang/p/12632579.html
Copyright © 2011-2022 走看看