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系列的第十五部分,我 ...
随机推荐
- 题解【洛谷P2279】[HNOI2003]消防局的设立
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有\(n\)个基地.起初为了节约材料,人类只修建了\(n-1\)条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成 ...
- Codeforces Round #604 (Div. 2)D(构造)
构造,枚举起点,如果一个序列成立,那么将它reverse依然成立,所以两个方向(从小到大或从大到小)没有区别,选定一个方向进行探测,直到探测不到以后回头,只要所给数据能成立,那么能探测进去就能探测出来 ...
- 时间类型:datetime,timestamp,date,time,year
时间类型 1.年月日时分秒:datetime #取值范围# '1000-01-01 00:00:00'到'9999-12-31 23:59:59' 占存储8B:表示的范围比timestamp大:支持0 ...
- Linux 设备驱动IO操作
每个外设都是通过读写其寄存器来控制的.外设寄存器也称为I/O端口,通常包括:控制寄存器.状态寄存器和数据寄存器三大类. 根据CPU体系结构的不同,CPU对IO端口的编址方式有两种: (1)I/O映射方 ...
- Bugku-CTF加密篇之贝斯家族(@iH<,{bdR2H;i6*Tm,Wx2izpx2!)
贝斯家族 @iH<,{bdR2H;i6*Tm,Wx2izpx2!
- python pylab.plot() 方法使用
Python 中用pylab模块, pylab.plot() 函数,绘制折线统计图 import pylab as pl x = [, , , ] y = [, , , ] ''' plot参数说明: ...
- Spring Boot Json 之 Jackjson Fastjson
Json 是目前互联网应用使用最为广泛的信息交换格式之一.Spring Boot 内置了 Jackson .Json 在应用中主要体现在以下功能: 序列化 反序列化 字段格式化 验证自动化 目前长用的 ...
- leetcode 697
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- jquery-1.10.2_d88366fd.js和jquery-3.1.0.min.js 在用touch事件时候, event.changedTouches[0]报错的问题。
1.animation动画:(注意如果这个动画是一开始就执行的,在pc端就要用px,在手机端用rem,如果在pc端展示页面,但用的是rem为单位,这时候动画一开始就执行,因为根字体大小还没准备好,动画 ...
- Vue-阻止页面回退
1.原生js方法 <script language="javascript"> //防止页面后退 使用在vue时 挂载到mounted中 history.pushSta ...