flask之Flask特殊装饰器

flask_decorators.py
'''
Flask中的特殊装饰器:
(1)@app.before_request
请求到达视图函数之前,进行自定义操作,类似django中间件中的process_request,在app中使用则为全局,在蓝图中使用则针对当前蓝图
注意正常状态下return值必须为None
(2)@app.after_request
响应返回到达客户端之前,进行自定义操作,类似jango中间件中的process_response,在app中使用则为全局,在蓝图中使用则针对当前蓝图
注意正常状态下视图函数必须定义一个形参接收response对象,并通过return response返回
(3)@app.errorhandler()
错误状态码捕获执行函数,装饰器参数务必是4xx或者5xx的int型错误状态码
(4)@app.template_global() :定义装饰全局模板可用的函数,直接可在模板中进行渲染使用
@app.template_filter(): 定义装饰全局模板可用的过滤器函数,类似django中的自定义过滤器,直接可在模板中使用
两个特殊装饰器主要用在模板渲染,详情使用见falsk学习中的jinjia2学习
'''
import os from flask import Flask, render_template, session, redirect, request, send_file app = Flask(__name__)
app.debug = True
app.secret_key = 'sdfghjhg1234' # (1)@app.before_request请求达到视图函数之前装饰器函数,正常状态务必return None
@app.before_request
def b1():
print('b1')
urls = ['/login']
if request.path in urls:
return None
if session.get('username'):
return None
else:
return redirect('/login') @app.before_request
def b2():
print('b2')
return None # (2)@app.after_request响应到达客户端之前装饰器函数,正常状态被装饰函数必须定义一个形参来接收response,务必return response
@app.after_request
def a1(response):
print('a1')
return response @app.after_request
def a2(response):
print('a2')
return response # (3)@app.errorhandler(错误状态码)错误捕获装饰器,装饰其中必须传入4xx或5xx的错误状态码,同时在被装饰函数中定义一个形参来接收错误信息error
@app.errorhandler(404)
def notfond(errormessage):
print(errormessage)
return send_file(os.path.join(os.path.dirname(__file__), 'static', '1.png')) # (4)@app.template_global()和@app.template_filter()装饰器函数直接在模板中可以全局使用 @app.template_global()
def sum1(a, b):
return a + b @app.template_filter()
def sum2(a, b, c, d):
return a + b + c + d # (5)@app.route()路由视图装饰器,第一个参数为请求路径,其它关键字参数使用相见flask之route路由学习
# 可以使用app.add_url_rule(rule, endpoint=None, view_func=None,)进行路由
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
print('login_get')
return render_template('login.html')
elif request.method == 'POST':
print('login_post')
username = request.form.get('username')
pwd = request.form.get('pwd')
if username == 'yang' and pwd == '':
session['username'] = username
return redirect('/index')
else:
return '登录失败!!!' @app.route('/index')
def index():
print('index')
return render_template('index.html') @app.route('/data')
def data():
print('data')
return 'data' @app.route('/detail')
def detail():
print('detail')
return 'detail' if __name__ == '__main__':
app.run()
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
index页面
</body>
</html>
flask之Flask特殊装饰器的更多相关文章
- flask笔记(三)Flask 添加登陆验证装饰器报错,及解析
Flask 添加登陆验证装饰器报错,及解析 写这个之前,是想到一个需求,这个是关于之前写Flask笔记(二)中的一个知识点,路由相关 需求为 : 有一些页面必须是登陆之后才能访问的,比如Shoppin ...
- Flask之基于route装饰器的路由系统(源码阅读解析)
一 路由系统 1. 在flask中配置URL和视图函数的路由时,首先需要在main.py中实例化一个app对象: from flask import Flask, render_template ap ...
- Flask中的before_request装饰器和after_request装饰器以及WTForms组件
一.before_request装饰器和after_request装饰器 我们现在有一个Flask程序其中有3个路由和视图函数 from flask import Flask app = Flask( ...
- flask模板语言,装饰器,路由及配置
1.模板语言jinja2 Flask中默认的模板语言是Jinja2 1.0 模板传参 from flask import Flask,render_template app = Flask(__nam ...
- Flask(2)- 装饰器的坑及解决办法、flask中的路由/实例化配置/对象配置/蓝图/特殊装饰器(中间件、重定义错误页面)
一.装饰器的坑以及解决方法 1.使用装饰器装饰两个视图函数,代码如下 from flask import Flask, redirect, render_template, request, sess ...
- Flask 模板语言,装饰器
Jinja2模板语言 # -*- coding: utf-8 -*- from flask import Flask, render_template, request, redirect, ...
- Flask框架视图多层装饰器问题
Flask中的app.route装饰器 我们知道,在flask框架中,我们的路由匹配就是通过有参装饰器来实现的,我们看一个简单的例子: from flask import Flask, render_ ...
- Flask内的特殊装饰器
@app.template_global() # 全局变量 @app.template_filter() # 偏函数 @app.before_request # 请求进入视图函数之前,比 ...
- Flask入门 之 没有装饰器的路由
有些时候,需要一个类似路由的功能,但又不能或者不想写装饰器,这该怎么办? so easy! eg: @app.route('login') def login(): return 'hello wor ...
- Flask 中的 特殊装饰器before_request/after_request
before_request :在请求收到之前绑定一个函数做一些事情. after_request: 每一个请求之后绑定一个函数,如果请求没有异常. teardown_request: 每一个请求之后 ...
随机推荐
- QT 无法抓住异常
出处:https://stackoverflow.com/questions/40980171/qt5core-dll-crashing I've found that enabling /EHa ( ...
- 将Spring Boot应用程序注册成为系统服务
文章目录 前期准备 打包成可执行jar包 注册成为liunx服务 System V Init Systemd Upstart 在Windows中安装 Windows Service Wrapper J ...
- JQuery学习(一)
本文是学习廖老师的Javascript全栈教程后的一些笔记. 使用jQuery: 方法一:下载jQuery库,并在html页面中引入,方式如下: 1 <html> 2 <head&g ...
- nginx平滑升级、在线添加模块(tengine 动态加载模块)
http://www.orzace.com/how-to-upgrade-nginx/ 下面是nginx替换成tengine再加上lua 模块,(tengine-2.0.1版本暂时无法动态加载lua模 ...
- JAVA第一次blog总结
JAVA第一次blog总结 0.前言 大一下学期我们开展了OPP这门课程,这也是我们第一次接触到JAVA.与上学期我们在学校里学C语言不同的是,这学期由于疫情原因我们是以网课的方式在学习.在学习中我发 ...
- Automatic Reference Counting
NSObject简化版alloc: struct obj_layout { NSUInteger retained; }; + (id)alloc { int size = sizeof(struct ...
- Collections集合工具类常用的方法
java.utils.Collections //是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Collection& ...
- 操作系统实验——PV操作实现生产者消费者模型
操作系统PV操作之--生产者消费者模型 个人博客主页 参考资料: Java实现PV操作 | 生产者与消费者 浙大公开课 在操作系统的多进程.多线程操作中经常会有因为同步.互斥等等问题引发出的一系列问题 ...
- flush方法和close方法的区别
package com.yhqtv.demo05.Writer; import java.io.FileWriter; /* * @author XMKJ yhqtv.com Email:yhqtv@ ...
- Semaphore和AQS
Semaphore意思的信号量,它的作用是控制访问特定资源的线程数量 构造方法: public Semaphore(int permits) public Semaphore(int permits, ...