栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Flask项目】蓝图的基本使用 # 5

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Flask项目】蓝图的基本使用 # 5

为防止代码冗余,Flask中蓝图能够让我们以模块化的方式对项目进行管理。

首先,从最基本的首页写起:

在info文件夹下创建modules文件夹,再在modules文件夹下创建index文件夹:

蓝图的基本使用:
# index文件夹__init__.py文件

from flask import Blueprint

blue_index = Blueprint("name", __name__)

from . import views
# index文件夹views.py文件

from . import blue_index
from flask import render_template


@blue_index.route("/")
def index():
    return render_template("index.html")

以上写完后,在 create_app 中注册蓝图以便使用:

def create_app(config_name):
    """...省略一堆之前写过的代码..."""

    from info.module.index import blue_index
    app.register_blueprint(blue_index)

    return app

未注册蓝图时打印app.url_map:

if __name__ == '__main__':
    print(app.url_map)
    manager.run()

注册后打印:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317286.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号