flask blueprint模块
Posted 你所信必为你所见
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask blueprint模块相关的知识,希望对你有一定的参考价值。
一.为什么要用蓝图?
1.试想,如果把所有的视图写在一个文件中,文件中的代码可能上万行,在协同开发中,在做版本控制时很容易冲突
2.如果要删除某个功能,那就要在一个文件中把相关的代码全部删除,因为耦合性太强,容易影响项目整体的运行
3.通过蓝图模块化管理,容易维护管理。
二.Blueprint使用
概念:一个蓝图定义了可用于单个应用的视图,模板,静态文件等等的集合。
简单项目结构:
核心代码:

from flask import Blueprint,render_template
account = Blueprint(\'account\',__name__)
@account.route(\'/login\')
def login():
return render_template(\'login.html\')

from flask import Blueprint
order = Blueprint(\'order\',__name__)
@order.route(\'/order\')
def login():
return \'Order\'

from flask import Flask
from .views import account
from .views import order
app = Flask(__name__)
# print(app.root_path)
app.register_blueprint(account.account)
app.register_blueprint(order.order)

import frm
if __name__ == \'__main__\':
frm.app.run(port=8001) #自定义端口
以上是关于flask blueprint模块的主要内容,如果未能解决你的问题,请参考以下文章