当前位置:网站首页>[pytho-flask笔记5]蓝图简单使用
[pytho-flask笔记5]蓝图简单使用
2022-07-23 05:38:00 【bigdata王一】
蓝图:
flask自带的模块,容器,存储了一组将来在程序上执行的操作,不能独立运行。可以有自己的静态文件和模板等;
类似于django中的子应用;
蓝图多文件使用:容易发生循环导入的问题
除了最基本的三步以外,必须把使用蓝图对象的视图文件,导入到创建蓝图对象的文件中
蓝图最简单案例-三步
from flask import Flask,Blueprint
# 蓝图使用步骤
#1 创建蓝图对象
#2 定义蓝图路由
#3 注册蓝图对象
app = Flask(__name__)
#1 创建蓝图对象
bp = Blueprint('bp',__name__)
#2 定义蓝图路由
@bp.route("/users")
def get_user():
return 'users ..'
@app.route("/")
def index():
return "hi"
#3 注册蓝图对象
app.register_blueprint(bp)
if __name__ == '__main__':
print(app.url_map)
app.run(debug=True)
多文件蓝图注意import导包死锁
目录结构:
不能同时互相导入对方的包,就像夹菜俩人一起夹菜,谁也没夹起来……程序报错importErro……
多文件在ini的py文件创建蓝图。在类的py文件写route路由(定义蓝图路由)。在调用的时候注册蓝图。启动后可以使用
多文件使用,必须把使用蓝图对象的视图文件,导入到创建蓝图对象的文件中
init.py
from flask import Blueprint
#创建蓝图
news_bp = Blueprint('news_bp',__name__)
#必须把使用蓝图对象的视图文件,导入到创建蓝图对象的文件中
from . import views
views.py
from . import news_bp
@news_bp.route("/news")
def get_news():
return "news demo .."
@news_bp.route("/user")
def get_user():
return "user 666"
bluePrint.py
from flask import Flask,Blueprint
app = Flask(__name__)
from news import news_bp
#注册蓝图对象
app.register_blueprint(news_bp)
@app.route("/")
def index():
return 'hi'
if __name__ == '__main__':
print(app.url_map)
app.run(debug=True)
效果图:
边栏推荐
- Master slave synchronization step read / write separation + self encountered error sharing
- Leetcode daily question (1946. largest number after varying substring)
- Web server failed to start. Port 8080 was already in use.
- 从零开始的pytorch小白使用指北
- 8. Surface geometry
- 【达人专栏】还不会用Apache Dolphinscheduler吗,大佬用时一个月写出的最全入门教学【二】
- Why does MySQL index use b+ tree?
- Project deployment (simplified version)
- 使用cmd安装pygame
- Partial usage of C #
猜你喜欢

Dictionary creation and copying

"The six programming languages I want most!"

RPC与thrift入门

安装企业版pycharm以及anaconda

讲师征集令 | Apache DolphinScheduler Meetup分享嘉宾,期待你的议题和声音!

cuda10.0配置pytorch1.7.0+monai0.9.0

Flask blueprint

给图片人脸部分加上马赛克

Detailed explanation of structure

6. Barycentric coordinate interpolation and graphics rendering pipeline
随机推荐
JDBC数据库连接池
大米商城注册
52832dongle installation
Compare the advantages and disadvantages of RDB and AOF modes of redis
从零开始的pytorch小白使用指北
大厂面试机器学习算法(6)时间序列分析
【无标题】
Mysql事务回滚机制与原理
同步发送短信验证码
3.Flask 中的线程
Cadence learning path (VIII) PCB placement components
[ROS advanced chapter] Lesson 8 syntax explanation of URDF file
Project deployment (simplified version)
Mysql database foundation
QT style syntax definition access, can check the data, can be automatically generated
防止神经网络过拟合的五种方法
6. Barycentric coordinate interpolation and graphics rendering pipeline
图片模糊处理批量生产模糊数据集
sprintboot中使用缓存时候,数据加载不出来
Murata power maintenance switch server power maintenance and main functional features