当前位置:网站首页>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>
继续努力,终成大器。
边栏推荐
- A case study on the collaborative management of medical enterprise suppliers, hospitals, patients and other parties by building a low code platform
- Cadence学习之路(八)PCB放置元器件
- Redis source code and design analysis -- 11. Hash object
- Redis源码与设计剖析 -- 7.快速列表
- pygame实现飞机大战游戏
- Mysql-8.0.28 user operation or user permission Operation
- Notes and Thoughts on the red dust of the sky (IV) invalid mutual value
- C#的partial用法
- Redis source code and design analysis -- 13. Ordered collection objects
- An accident caused by MySQL misoperation, and "high availability" is not working well
猜你喜欢

Chapter 1 Overview - Section 1 - 1.2 overview of the Internet

达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程

Redis source code and design analysis -- 7. Quick list

9、光线追踪

MySql语句查询某一级节点的所有子节点

Cadence学习之路(八)PCB放置元器件

Powerbi Getting Started Guide

Xssgame games (XSS learning) level1-15

Mysql database foundation

Redis源码与设计剖析 -- 6.压缩列表
随机推荐
3DMAX first skin brush weights, then attach merge
Updated again, idea 2022.2 officially released
Chapter 1 Overview - Section 1 - 1.2 overview of the Internet
Deploy storageclass trample record
Redis源码与设计剖析 -- 9.字符串对象
Gerrit 使用操作手册
Mysql database foundation
Redis source code and design analysis -- 14. Database implementation
UNITY VFX syntax error: unexpected token ‘#‘ at kernel CSMain
Fundamentals of software testing - design method of test cases
Cadence (IX) 17.4 rules and spacing settings
img标签设置height和width无效
pygame实现飞机大战游戏
Web server failed to start. Port 8080 was already in use.
PMP practice once a day | don't get lost in the exam -7.22
Analyse du code source et de la conception de redis - - 7. Liste rapide
讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!
C ivalueconverter interface usage example
Meyer burger Siemens industrial computer maintenance and machine tool maintenance
“我最想要的六种编程语言!”