Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例


Posted in Python onAugust 01, 2019

本文实例讲述了Flask框架重定向,错误显示,Responses响应及Sessions会话操作。分享给大家供大家参考,具体如下:

重定向和错误显示

将用户重定向到另一个端点,使用redirect(), 要提前中止错误请求,请使用abort()函数

from flask import abort, redirect, url_for
@app.route('/')
def index():
  return redirect(url_for('login'))
@app.route('/login')
def login():
  abort(401)
  this_is_never_executed()

默认情况下,会为每个错误代码显示黑白错误页面,如果要自定义错误页面,请使用errorhandler() 装饰器.

Responses

  1. 如果返回了正确类型的响应对象,则直接从视图返回。
  2. 如果是字符串,则使用该数据和默认参数创建响应对象。
  3. 如果返回元组,则元组中的项可以提供额外信息。这样的元组必须是这样的形式,或者至少有一个项必须在元组中。该值将覆盖状态代码,可以是其他标头值的列表或字典。(response, status, headers)或者是(response, headers)

如果要在视图中获取生成的响应对象,可以使用make_response() 函数

假设你有如下视图:

@app.errorhandler(404)
def not_found(error):
  return render_template('error.html'), 404

使用make_response()包含返回表达式,获取响应对象并修改它,然后返回它

@app.errorhandler(404)
def not_found(error):
  resp = make_response(render_template('error.html'), 404)
  resp.headers['X-Something'] = 'A value'
  return resp

Sessions会话追踪

session在cookie的基础上实现的,并以加密方式对cookie进行签名

要使用sessions,必须要设置私钥,以下是简单示例:

from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
@app.route('/')
def index():
  if 'username' in session:
    return 'Logged in as %s' % escape(session['username'])
  return 'You are not logged in'
@app.route('/login', methods=['GET', 'POST'])
def login():
  if request.method == 'POST':
    session['username'] = request.form['username']
    return redirect(url_for('index'))
  return '''
    <form method="post">
      <p><input type=text name=username>
      <p><input type=submit value=Login>
    </form>
  '''
@app.route('/logout')
def logout():
  # remove the username from the session if it's there
  session.pop('username', None)
  return redirect(url_for('index'))

希望本文所述对大家基于flask框架的Python程序设计有所帮助。

Python 相关文章推荐
仅用50行Python代码实现一个简单的代理服务器
Apr 08 Python
Python3实现抓取javascript动态生成的html网页功能示例
Aug 22 Python
Python实现PS滤镜的万花筒效果示例
Jan 23 Python
python模块之paramiko实例代码
Jan 31 Python
Python网页正文转换语音文件的操作方法
Dec 09 Python
Python 读取串口数据,动态绘图的示例
Jul 02 Python
Python打包工具PyInstaller的安装与pycharm配置支持PyInstaller详细方法
Feb 27 Python
浅谈在JupyterNotebook下导入自己的模块的问题
Apr 16 Python
jupyternotebook 撤销删除的操作方式
Apr 17 Python
Python使用pyexecjs代码案例解析
Jul 13 Python
快速一键生成Python爬虫请求头
Mar 04 Python
Python集合set()使用的方法详解
Mar 18 Python
python对csv文件追加写入列的方法
Aug 01 #Python
Django Aggregation聚合使用方法解析
Aug 01 #Python
Flask教程之重定向与错误处理实例分析
Aug 01 #Python
python gdal安装与简单使用
Aug 01 #Python
Django模型修改及数据迁移实现解析
Aug 01 #Python
Django 大文件下载实现过程解析
Aug 01 #Python
python爬虫刷访问量 2019 7月
Aug 01 #Python
You might like
PHP+SQL 注入攻击的技术实现以及预防办法
2011/01/27 PHP
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
2012/06/17 PHP
PHP反射类ReflectionClass和ReflectionObject的使用方法
2013/11/13 PHP
jquery 插件 人性化的消息显示
2008/01/21 Javascript
script标签的 charset 属性使用说明
2010/12/04 Javascript
jQuery中(function(){})()执行顺序的理解
2013/03/05 Javascript
深入理解Javascript作用域与变量提升
2013/12/09 Javascript
js的延迟执行问题分析
2014/06/23 Javascript
javascript验证身份证号
2015/03/03 Javascript
详解JavaScript对Date对象的操作问题(生成一个倒数7天的数组)
2015/10/01 Javascript
jQuery Easyui学习教程之实现datagrid在没有数据时显示相关提示内容
2016/07/09 Javascript
JS回调函数基本定义与用法实例分析
2017/05/24 Javascript
jQuery实现节点的追加、替换、删除、复制功能示例
2017/07/11 jQuery
详解用函数式编程对JavaScript进行断舍离
2017/09/18 Javascript
jQuery内容过滤选择器与子元素过滤选择器用法实例分析
2019/02/20 jQuery
js+css实现扇形导航效果
2020/08/18 Javascript
python动态监控日志内容的示例
2014/02/16 Python
Python如何发布程序的详细教程
2018/10/09 Python
Python3实现对列表按元组指定列进行排序的方法分析
2018/12/22 Python
简单了解python的break、continue、pass
2019/07/08 Python
Flask框架钩子函数功能与用法分析
2019/08/02 Python
使用OpenCV-python3实现滑动条更新图像的Canny边缘检测功能
2019/12/12 Python
pytorch 利用lstm做mnist手写数字识别分类的实例
2020/01/10 Python
浅谈Pycharm最有必要改的几个默认设置项
2020/02/14 Python
Django 项目通过加载不同env文件来区分不同环境
2020/02/17 Python
Anaconda+vscode+pytorch环境搭建过程详解
2020/05/25 Python
Python 利用argparse模块实现脚本命令行参数解析
2020/12/28 Python
CSS3 2D模拟实现摩天轮旋转效果
2016/11/16 HTML / CSS
英国50岁以上人群的交友网站:Ourtime
2018/03/28 全球购物
英国领先的维生素和补充剂品牌:Higher Nature
2019/08/26 全球购物
音乐教学案例
2014/01/30 职场文书
私营公司诉讼代理委托书范本
2014/09/13 职场文书
实习证明格式范文
2014/10/14 职场文书
师德承诺书
2015/01/20 职场文书
亲情作文之母爱
2019/09/25 职场文书
关于Python中进度条的六个实用技巧分享
2022/04/05 Python