flask blueprints
flask blueprints
1. flask blueprints
蓝图是一种网页模板的组合,可以方便的注册到flask中。
蓝图可以在文件中声明,也可以在包中声明,一般而言推荐在包中声明(下文如是)。
1.1. 基本使用
声明多个蓝图后的目录结构:
app/
bluep #蓝图文件夹1
blueo #蓝图文件夹2
app.py
蓝图声明案例:
__init__.py
from flask import Blueprint
blue_t = Blueprint('blue_t', __name__)
from . import views
views.py
#coding:utf-8
__author__ = 'sss'
……
from . import blue_t
@blue_t.route('/')
@blue_t.route('/index')
def index():
#print(blue_t.template_folder)
#print(blue_t.root_path)
return render_template('bluep/index.html', title='我的', user=user, posts=posts)
# 登录
@blue_t.route('/login', methods=['GET', 'POST'])
def login():
…
@blue_t.route('/lougout')
def logout():
logout_user()
return redirect(url_for('blue_t.index'))
@blue_t.errorhandler(404)
def error_handle(error):
return render_template('error/404.html')
注册:app.py
from .bluep import blue_t as blueprint_test
app.register_blueprint(blueprint_test, url_prefix='/blue')
访问:
http://127.0.0.1:9000/blue/index
url_prefix声明了附加路径。
1.2.
常用参数
1.2.1.
资源路径
蓝图的资源路径从它的__name__参数中推导而来。
可以通过Blueprint.root_path属性查看。
1.2.2.
static files
admin = Blueprint('admin', __name__, static_folder='static')
1.2.3.
templates folder
蓝图有两种组织形式
- 蓝图有自己的资源文件夹:
- 所有蓝图共用一个资源文件夹:
主要的不同是template_folder参数设置的不同。
- 有自己的资源文件夹
蓝图需要建在一个目录下
blue_t = Blueprint('blue_t', __name__, template_folder=r'templates\bluep')
这时蓝图会去app\bluep\templates\bluep下寻找模板文件。
- 共用原始资源文件夹
蓝图不需要建在目录下
但需要注意写法的不同:render_template(‘bluep/index.html’)
这时蓝图会去app\templates\bluep目录下寻找模板文件
需要注意的是在模板文件中引用的其它模板文件如未指明路径,仍会在app\templates下寻找。
资源目录可通过属性blue.root_path查看。template_folder会附加在后部。
资源目录可以是绝对路径也可以是相对路径,但蓝图的资源目录级别是低于应用资源目录的。
1.2.4.
url_for
蓝图的endpoint可以理解为:
蓝图名.函数名
url_for是通过endpoint查询url地址,然后找视图函数
return redirect(url_for('blue_t.index'))
如果在同一蓝图下也可以这样:
url_for('.index')
flask blueprints的更多相关文章
- Inside Flask - flask.__init__.py 和核心组件
Inside Flask - flask.__init__.py 和核心组件 简单的示例 首先看看一个简单的示例.使用 Flask ,通常是从 flask 模块导入 Flask . request 等 ...
- [Python]Flask构建网站分析应用
原文Saturday morning hacks: Building an Analytics App with Flask - 由orangleliu友情翻译 ,主要是通过埋点技术来实现web网页的 ...
- Flask源码阅读-第四篇(flask\app.py)
flask.app该模块2000多行代码,主要完成应用的配置.初始化.蓝图注册.请求装饰器定义.应用的启动和监听,其中以下方法可以重点品读和关注 def setupmethod(f): @setupm ...
- flask基础之蓝图的使用(七)
前言 关于蓝图是什么?或为什么使用蓝图的详细介绍,官方文档讲的很详细,不再赘述.简单来说,在大型的应用中,我们不想视图函数显得杂乱无章,难以维护,将众多的视图函数按照Api的设计规则进行切割是一个好方 ...
- How Flask Routing Works
@How Flask Routing Works The entire idea of Flask (and the underlying Werkzeug library) is to map UR ...
- Awesome Flask
Awesome Flask A curated list of awesome Flask resources and plugins Awesome Flask Framework Admin i ...
- Awesome Flask Awesome
A curated list of awesome Flask resources and plugins Awesome Flask Framework Admin interface Authen ...
- web开发工具flask中文英文书籍-持续更新
web开发工具flask中文英文书籍-持续更新 python测试开发_AI命理关注 0.9222018.11.10 07:48:43字数 625阅读 885 python测试开发项目实战-目录 pyt ...
- Flask 教程 第十五章:优化应用结构
本文翻译自The Flask Mega-Tutorial Part XV: A Better Application Structure 这是Flask Mega-Tutorial系列的第十五部分,我 ...
随机推荐
- Linux /dev/sda1磁盘满了,清理办法
转:https://blog.csdn.net/h_8410435/article/details/86303995 查看内存使用情况 df -lh Filesystem Size Use ...
- Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)
预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 #d ...
- 记一道简单的re--BUUctf reverse1
1.首先拖进ida里,看到了左面一百多function...还是shift+f12 查看敏感字符串吧 2.发现了这两个比较可疑的字符串,然后双击this is the right flag 进入到了他 ...
- jmeter实现IP欺骗
用jmeter模拟多个IP同时向一个目标发送请求 1.IP地址参数化 在csv文件中编辑参数化IP地址列表,参数化的IP需在同一个局域网,子网掩码相同(比如和客户端本机同一网段),如下 将csv列表中 ...
- redis环境搭建学习笔记
学习环境为windows.java环境 一.学习教程: 1.菜鸟教程:http://www.runoob.com/redis/redis-tutorial.html 2.redis中文网:http:/ ...
- shell查找七天之前的文件
#!/bin/bashaweekago=`date -d "7 days ago" +%s`for f in $(ls) do stat -c %Y ${f} aa=`stat - ...
- htm5实现 文件夹上传
观察百度云上传,可以看到input type=file 的标签上加个一个属性“webkitdirectory”,加上次属性即可选择整个文件夹.
- IDEA 导出可执行jar包【转载】
1,在项目上鼠标右键 --> Open Module Settings,或者[shift+ctrl+alt+s] 2, Artifacts --> + --> JAR --> ...
- css——伪类选择器
<body> <div class="box"> <p>0</p> <div>1</div&g ...
- Sqoop的安装及常用命令
本次安装主要是为了离线分析数据清洗完成后的操作:网站日志流量分析系统之数据清洗处理(离线分析) 一.概述 1. sqoop是Apache 提供的工具,用于hdfs和关系型数据库之间数据的导入和导入 2 ...