当前位置:网站首页>[python flask notes 5] blueprint is easy to use
[python flask notes 5] blueprint is easy to use
2022-07-23 11:13:00 【Bigdata Wang Yi】
List of articles
The blueprint :
flask Built in modules , Containers , It stores a set of operations to be performed on the program in the future , Can't run independently . You can have your own static files and templates ;
Be similar to django Subapplications in ;
Blueprint multi document use : It is easy to have the problem of circular import
In addition to the most basic three steps , You must use the view file of the blueprint object , Import into the file that creates the blueprint object
The simplest case of blueprint - The three step
from flask import Flask,Blueprint
# Blueprint use steps
#1 Create blueprint object
#2 Define blueprint routes
#3 Register blueprint objects
app = Flask(__name__)
#1 Create blueprint object
bp = Blueprint('bp',__name__)
#2 Define blueprint routes
@bp.route("/users")
def get_user():
return 'users ..'
@app.route("/")
def index():
return "hi"
#3 Register blueprint objects
app.register_blueprint(bp)
if __name__ == '__main__':
print(app.url_map)
app.run(debug=True)
Attention to multi document blueprints import Guide Package deadlock
Directory structure :
You can't import each other's packages at the same time , It's like cooking with two people , No one caught it …… Application error importErro……
Multiple files in ini Of py Document creation blueprint . In class py The document says route route ( Define blueprint routes ). Register the blueprint when calling . It can be used after startup
Use multiple files , You must use the view file of the blueprint object , Import into the file that creates the blueprint object
init.py
from flask import Blueprint
# Create a blueprint
news_bp = Blueprint('news_bp',__name__)
# You must use the view file of the blueprint object , Import into the file that creates the blueprint object
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
# Register blueprint objects
app.register_blueprint(news_bp)
@app.route("/")
def index():
return 'hi'
if __name__ == '__main__':
print(app.url_map)
app.run(debug=True)
design sketch :
边栏推荐
- Spark常见面试问题整理
- Activiti工作流使用之流程结构介绍
- PyGame realizes the airplane war game
- Flask blueprint
- Fundamentals of software testing - design method of test cases
- Flask蓝图
- Notifier Nordic fire engine power supply maintenance and daily maintenance
- cuda10.0配置pytorch1.7.0+monai0.9.0
- mysql语法(纯语法)
- Fun code rain, share it online~-
猜你喜欢
随机推荐
面试必备之数据库专题
[untitled]
A usage exploration of entitymanagerfactory and entitymanager
The super simple face recognition API can realize face recognition in just a few lines of code
RPC与thrift入门
Hyperlink de underlined code
机器学习零散笔记:一些概念和注意
【无标题】
mysql invalid conn排查
支付宝DTS架构
【系统问题】.NET Framework 3.5 安装错误
cuda10.0配置pytorch1.7.0+monai0.9.0
【6.28】
idea中复制一个项目/project
JDBC数据库连接池
Notifier Nordic fire engine power supply maintenance and daily maintenance
【Pyradiomics】提取的影像组学特征值不正常(很多0和1)
【无标题】
MySQL-8.0.28 用户操作 or 用户权限操作
check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ord









