Python Web 5 —— 错误捕获和错误页
2016-07-03 22:33
483 查看
我们知道有时候可以会出现非正确路由范围的情况,为了防止程序挂掉,我们需要做一些容错的处理,这里就需要进行错误码的捕获了,flask提供了这样的接口:
@app.errorhandler(404)
def flask_error(e):
return render_template('404.html') 这里使用app.errorhandler(错误码)来捕获错误码,我们以404错误为例,此时处理的逻辑其实与普通路由相同,返回一个html字符串或者使用render_template渲染一个html文件作为返回结果,这里我们创建一个404.html作为错误显示界面:
在浏览器中输入一个以http://127.0.0.1:5000为前缀的非法访问地址,例如:http://127.0.0.1:5000/bootstrap/linshuhe/125可以看到结果:
@app.errorhandler(404)
def flask_error(e):
return render_template('404.html') 这里使用app.errorhandler(错误码)来捕获错误码,我们以404错误为例,此时处理的逻辑其实与普通路由相同,返回一个html字符串或者使用render_template渲染一个html文件作为返回结果,这里我们创建一个404.html作为错误显示界面:
{% extends "base.html" %} {% block title %}Flasky{% endblock %} {% block page_content %} <div class="page-header"> <h1>404 Error!</h1> </div> {% endblock %}
在浏览器中输入一个以http://127.0.0.1:5000为前缀的非法访问地址,例如:http://127.0.0.1:5000/bootstrap/linshuhe/125可以看到结果:
相关文章推荐
- python开发之路Day17-算法设计(冒泡排序、选择排序、插入排序、二叉树)
- Python 语言及其应用 Chapter_5_Note_1 模块
- python下如何安装.whl包?
- Python random模块
- scikit-learn : LARS
- 三种排序算法的python实现+装饰器统计每种算法耗时
- 《Python数据分析基础教程:Numpy学习指南》 常用模块与方法简要记录
- 初识python 4
- scikit-learn : LASSO
- python3自动登录
- Python学习笔记之列表
- Python Web 4 —— 让页面变好看点BootScrap插件
- OpenCV—Python 学习笔记
- Python学习-文件操作
- 231. Power of Two python
- python 学习简记 《编程导论》 CH10&CH11
- Python学习-函数补充2-内置函数
- 利用PyCharm进行Python远程调试
- Python 语言及其应用 Chapter_4_Exercise
- python出现过的问题总结