当前位置:网站首页>Flask蓝图
Flask蓝图
2022-07-23 05:38:00 【紫青宝剑】
Flask蓝图
概述:蓝图分为两种方式,按照功能划分,与按照结构划分。
1.小蓝图
说明:按照功能划分蓝图。
在视图函数中创建多个 py 文件,每个 py 文件中的视图函数都可以注册到蓝图中;
目录结构如图所示:

manage.py 为启动文件:
# -*- coding: utf-8 -*-
from pythonProject import create_app
app = create_app() # 调用文件创建 app
if __name__ == '__main__':
app.run()
pythonProject /__init__.py文件是包的标志文件。通常说的导入一个包即为导入它的__init__.py文件;因此我们只需要在__init__.py中创建对应的 app 即可。
from flask import Flask
from .views.account import ac_blueprint
def create_app():
app = Flask(__name__)
app.register_blueprint(ac_blueprint,url_prefix = '/web')
# /web 相当于路由分发的前置
return app
在小蓝图内编写视图函数
# -*- coding: utf-8 -*-
from flask import Blueprint
ac_blueprint = Blueprint('ac',__name__)
# 将路由注册到蓝图内
@ac_blueprint.route('/f1')
def func():
return "f1"

2.大蓝图
说明:按照结构划分蓝图。
目录结构
**补充:**apps 有的时候也可能是与项目名称相同。

# -*- coding: utf-8 -*-
from apps import create_app
app = create_app()
if __name__ == '__main__':
app.run()# 启动文件
创建 app 函数,注册蓝图。
from flask import Flask
from .account import account
from .admin import admin
def create_app():
app = Flask(__name__)
app.config.from_object('config.settings') # 加载配置文件
app.register_blueprint(account)# 注册蓝图
app.register_blueprint(admin) # 注册蓝图
return app
在每个项目中创建蓝图
# -*- coding: utf-8 -*-
from flask import Blueprint
admin = Blueprint("ad",__name__)
# -*- coding: utf-8 -*-
from flask import Blueprint
account = Blueprint("ac",__name__,template_folder='templates')
# 创建蓝图,并指定相关的参数,更加具体的参数可以参考源码示例
from .views import user
# 注意需要写到蓝图对象下面,不然导入的时候蓝图对象不会被加载到内存
源码示例

蓝图中视图函数的写法
# -*- coding: utf-8 -*-
from flask import render_template
from .. import account # 导入蓝图,因此需要在蓝图被创建后将视图模块导入。
@account.route('/login')
def login():
return render_template('login.html')
相关 html 文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>内部登录</h1>
</body>
</html>
继续努力,终成大器。
边栏推荐
- Redis source code and design analysis -- 9. String object
- Notes and Thoughts on the red dust of the sky (IV) invalid mutual value
- 比特,位,字节,字的概念与区别
- 3DMAX first skin brush weights, then attach merge
- 对比redis的RDB、AOF模式的优缺点
- Activiti工作流使用之新建bpmn文件
- 达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程
- LearnOpenGL - Introduction
- C ivalueconverter interface usage example
- 9. Ray tracing
猜你喜欢

Notifier Nordic fire engine power supply maintenance and daily maintenance

Redis source code and design analysis -- 6. Compressed list

H1 -- HDMI interface test application 2022-07-15

Redis源碼與設計剖析 -- 7.快速列錶

使用cmd安装pygame

Redis源码与设计剖析 -- 13.有序集合对象

52832Dongle的安装

SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION

Meyer Burger梅耶博格西门子工控机维修及机床养护

A case study on the collaborative management of medical enterprise suppliers, hospitals, patients and other parties by building a low code platform
随机推荐
Notes and Thoughts on the red dust of the sky (IV) invalid mutual value
Dynamic memory management
PyTorch(五)——PyTorch进阶训练技巧
Two strategies for building AI products / businesses (by Andrew ng)
视、音频分开的网站内容如何合并?批量下载代码又该如何编写?
SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION
单点登录-认证服务器与客户端的session过期时间如何统一
8. Surface geometry
Redis源码与设计剖析 -- 10.列表对象
疫情时期加中年危机——游荡在十字街口的三个月
Déploiement du projet (version abrégée)
7、纹理映射
6. Barycentric coordinate interpolation and graphics rendering pipeline
NOTIFIER诺帝菲尔消防主机电源维修及日常维护
Selenium JD crawler
Mysql事务回滚机制与原理
[visual slam] orb slam: tracking and mapping recognizable features
【社媒营销】出海新思路:Whatsapp Business替代Facebook
52832dongle installation
Understand asp Net core - Cookie based authentication