Flask教程之重定向与错误处理实例分析
作者:EthanSun 时间:2021-02-04 08:00:49
本文实例讲述了Flask教程之重定向与错误处理。分享给大家供大家参考,具体如下:
Flask类有一个redirect()
方法. 当我们调用它时, 它会返回一个响应对象并且按指定的状态码将用户重定向到另一个位置.
redirect()
方法的原型如下:
Flask.redirect(location, statuscode, response)
在上面的函数中,
location 我们要重定向的URL位置
statuscode 状态码会在http头中发送给浏览器, 默认为302
response 用这个参数来初始化响应对象
下面是一些标准的状态码:
HTTP_300_MULTIPLE_CHOICES
HTTP_301_MOVED_PERMANENTLY
HTTP_302_FOUND
HTTP_303_SEE_OTHER
HTTP_304_NOT_MODIFIED
HTTP_305_USE_PROXY
HTTP_306_RESERVED
HTTP_307_TEMPORARY_REDIRECT
重定向的状态码默认是302, 也就是已找到(found).
在下面的例子中, redirect()
方法用于当登录失败的时候再次展示登录页面.
from flask import Flask, redirect, url_for, render_template, request
# Initialize the Flask application
app = Flask(__name__)
@app.route('/')
def index():
return render_template('log_in.html')
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST' and
request.form['username'] == 'admin' :
return redirect(url_for('success'))
return redirect(url_for('index'))
@app.route('/success')
def success():
return 'logged in successfully'
if __name__ == '__main__':
app.run(debug = True)
Flask类有个abort()
方法, 参数包括一个错误状态码.
Flask.abort(code)
400 错误请求
401 未授权请求
403 禁止
404 找不到资源
406 不可接受
415 未知媒体类型
429 太多请求
我们来对上面代码中的login()
函数做一点修改. 如果我们在登录失败时不重新展示登录页, 而是展示"未授权"页面, 那么我们需要把重定向那行代码替换为abort(401)
.
from flask import Flask, redirect, url_for, render_template, request, abort
app = Flask(__name__)
@app.route('/')
def index():
return render_template('log_in.html')
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
if request.form['username'] == 'admin' :
return redirect(url_for('success'))
else:
abort(401)
else:
return redirect(url_for('index'))
@app.route('/success')
def success():
return 'logged in successfully'
if __name__ == '__main__':
app.run(debug = True)
希望本文所述对大家基于flask框架的Python程序设计有所帮助。
来源:https://segmentfault.com/a/1190000011441932
标签:Flask,重定向,错误处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详述如何提高MySQL中数据装载效率
2009-10-26 10:27:00
八种获取当前日期的js代码
2007-09-07 10:26:00
Python安装Graphviz 超详细图文教程
2023-02-27 16:11:18
![](https://img.aspxhome.com/file/2023/1/75891_0s.jpg)
教你用python实现12306余票查询
2021-03-16 01:39:19
![](https://img.aspxhome.com/file/2023/7/75167_0s.png)
简单的水果风暴的算法过程
2009-12-13 15:49:00
asp如何使用MsChart制图工具?
2010-06-05 12:40:00
Python matplotlib生成图片背景透明的示例代码
2022-07-04 06:22:57
![](https://img.aspxhome.com/file/2023/2/81472_0s.png)
python 布尔注入原理及渗透过程示例
2022-11-21 01:04:21
![](https://img.aspxhome.com/file/2023/8/83968_0s.jpg)
深入理解python对json的操作总结
2022-04-08 20:36:01
![](https://img.aspxhome.com/file/2023/9/99009_0s.png)
如何实现某一页面只让特定的用户浏览?
2010-05-19 21:44:00
js表格拖选动态效果COOL而实用
2007-08-05 12:07:00
![](https://img.aspxhome.com/file/UploadPic/up/2007080512171065.gif)
浅谈LogMiner的使用方法
2009-02-28 11:12:00
8个asp生成随机字符的函数
2007-08-04 10:17:00
![](https://img.aspxhome.com/file/UploadPic/20078/19/2007819193914352s.jpg)
python求素数示例分享
2021-06-16 12:01:48
python实现线性回归算法
2021-04-11 12:36:48
![](https://img.aspxhome.com/file/2023/3/99283_0s.jpg)
SQL学习笔记四 聚合函数、排序方法
2011-09-30 11:33:29
Django实现CAS+OAuth2的方法示例
2023-01-26 17:32:46
C#数据导入/导出Excel文件及winForm导出Execl总结
2023-07-18 04:04:43
超半数中文网页一年内将“消失”
2008-03-08 12:49:00
快速认识CSS中的overflow属性
2009-05-29 16:36:00
![](https://img.aspxhome.com/file/UploadPic/up/2009052916595412.gif)