Flask 蓝图的基本使用
1. 蓝图简介@H_404_14@
@H_502_5@随着 Flask 程序越来越复杂,需要对程序进行模块化的处理,蓝图 (Blueprint) 是 Flask 程序的模块化处理机制,它是一个存储视图方法的集合,Flask 程序通过 Blueprint 来组织 URL 以及处理请求。
2.1 功能概述
@H_502_5@程序将 /news/society/ 和 /news/tech/ 的相关功能组成一个蓝图 news;程序将 /products/car/ 和 /products/baby/ 的相关功能组成一个蓝图 products。
@H_502_5@
@H_502_5@假设访问的页面路径是 /products/car,Flask 框架在蓝图 news 和蓝图 products 中查找匹配该页面路径的路由,发现在蓝图 products 中,存在和路径 /products/car 相应的处理函数 car_products,最后将请求转发给函数 car_products 处理。
-
app.py
,程序的主文件; -
news.py
,实现蓝图 news; -
products.py
,实现蓝图 products。
2.2 主程序 app.py
@H_502_5@首先编写主程序
app.py
:
from flask import Flask
import news
import products
app = Flask(__name__)
app.register_blueprint(news.blueprint)
app.register_blueprint(products.blueprint)
app.run(debug = True)
@H_502_5@在第 2 行,导入模块
news.py
,在 news.py
中定义了一个蓝图对象 news.blueprint;在第 3 行,导入模块 products.py
,在 products.py
中定义了一个蓝图对象 products.blueprint。
2.3 蓝图 news.py
from flask import Blueprint
blueprint = Blueprint('news', __name__, url_prefix='/news')
@blueprint.route("/society/")
def society_news():
return "社会新闻版块"
@blueprint.route("/tech/")
def tech_news():
return "IT 新闻板块"
@H_502_5@在第 5 行,将路径 /society/ 和函数 society_news 关联;在第 9 行,将路径 /tech/ 和函数 tech_news 关联。注意:页面的绝对路径是 /news/society/ 和 /news/tech/,因为蓝图的 url_prefix 设置为 news,在蓝图内部,页面的相对路径是 /society/ 和 /tech/。
2.4 蓝图 products.py
from flask import Blueprint
blueprint = Blueprint('products', __name__, url_prefix='/products')
@blueprint.route("/car")
def car_products():
return "汽车产品版块"
@blueprint.route("/baby")
def baby_products():
return "婴儿产品版块"
@H_502_5@在第 3 行,创建一个名为 ‘products’ 的蓝图,该蓝图中页面的 URL 前缀为 /products;在第 5 行,将路径 /car/ 和函数 car_products 关联;在第 9 行,将路径 /baby/ 和函数 baby_products 关联。
@H_502_5@注意:页面的绝对路径是 /products/car/ 和 /product/baby/,因为蓝图的 url_prefix 等于 products,在蓝图内部,页面的相对路径是 /car/ 和 /baby/。
2.5 运行程序
@H_502_5@
2.6 源代码下载
3. 更具扩展性的架构@H_404_14@
3.1 概述
@H_502_5@第 2 小节的程序采用的就是这种架构,程序包含 2 个蓝图: news 和 products,由 3 个文件构成:
app.py
、news.py
、products.py
,其中 news.py
实现新闻版块,products.py
实现产品版块。
3.2 模板文件寻找规则
3.3 静态文件寻找规则
4. 具有扩展性的架构的例子@H_404_14@
4.1 目录结构
@H_502_5@
4.2 实现 app.py
@H_502_5@首先实现主程序
app.py
:
from flask import Flask
import news
import products
app = Flask(__name__)
app.register_blueprint(news.blueprint)
app.register_blueprint(products.blueprint)
app.run(debug = True)
@H_502_5@在第 2 行,导入模块
news.py
,在 news.py
中定义了一个蓝图对象 news.blueprint;在第 3 行,导入模块 products.py
,在 products.py
中定义了一个蓝图对象 products.blueprint。
4.3 实现 news/__init.py__
from flask import Blueprint, render_template
blueprint = Blueprint('news', __name__, url_prefix='/news', template_folder='templates', static_folder='static')
@blueprint.route("/society/")
def society_news():
return render_template('society.html')
@blueprint.route("/tech/")
def tech_news():
return "IT 新闻板块"
@H_502_5@在第 7 行,调用 render_template (‘society.html’) 渲染模板文件 society.html,根据模板文件的查找规则,最终在 ‘项目目录 /news/templates’ 目录下找到模板文件。
4.4 实现 news/templates/society.html
<link rel="stylesheet" href="{{ url_for('news.static',filename='news.css')}}">
<h1>社会新闻</h1>
@H_502_5@在模板文件中引用了静态文件 news.css。{{url_for (‘news.static’,filename=‘news.css’) }} 的输出为 news/static/news.css,其中 news.static 表示蓝图 news 的 static 目录。
4.5 实现 news/static/news.css
h1 {
color: red;
}
4.6 实现 products/__init.py__
from flask import Blueprint
blueprint = Blueprint('products', __name__, url_prefix='/products')
@blueprint.route("/car")
def car_products():
return "汽车产品版块"
@blueprint.route("/baby")
def baby_products():
return "婴儿产品版块"
4.7 运行程序
@H_502_5@