蓝图有自己的目录,它的所有资源都在其目录下。蓝图的资源目录是由创建Blueprint对象时传入的模块名”__name__”所在的位置决定的。同时,我们可以指定蓝图自己的模板目录和静态目录。比如我们创建蓝图时传入:
admin_bp = Blueprint(‘admin‘, __name__, template_folder=‘templates‘, static_folder=‘static‘)
这样,该蓝图的模板目录就在”admin/templates”下,而静态目录就在”admin/static”下。当然,其实默认值就是这两个位置,不指定也没关系。我们可以用蓝图对象的”root_path”属性获取其主资源路径,”open_resource()”方法访问主资源路径下的某个文件,比如:
# 假设 current app 在路径 /home/bjhee/flask-app, #这个将会返回 /home/bjhee/flask-app/admin print admin_bp.root_path # 读取文件 /home/bjhee/flask-app/admin/files/info.txt with admin_bp.open_resource(‘files/info.txt‘) as f: info = f.read() print info
__init__.py文件中创建蓝图对象users=Blueprint(‘users‘,__name__)
2.在这个蓝图目录下, 创建views.py文件,保存当前蓝图使用的视图函数
@admin.route(‘/‘) def home(): return ‘user.home‘
3 在users/init.py中引入views.py中所有的视图函数
from flask import Blueprint # 等同于原来在 manage.py里面的 app = Flask() users=Blueprint(‘users‘,__name__) from .views import *
4 在主应用main.py文件中的app对象上注册这个users蓝图对象
from users import users app.register_blueprint(users,url_prefix=‘/users‘)
当这个应用启动后,通过/users/可以访问到蓝图中定义的视图函数
小型应用程序文件结构
/ yourapplication / yourapplication / __init__.py / views __init__.py admin.py frontend.py / static / style.css / templates layout.html index.html login.html .../ yourapplication __init__.py / apps __init__.py / frontend __init__.py views.py / static style.css / templates index.html about.html ... / admin __init__.py views.py / static style.css / templates list_items.html show_item.html ...其他:
原文:https://www.cnblogs.com/qiu-hua/p/12814265.html