Flask框架学习笔记之消息提示与异常处理操作详解
技术  /  管理员 发布于 7年前   365
本文实例讲述了Flask框架学习笔记之消息提示与异常处理操作。分享给大家供大家参考,具体如下:
flask通过flash方法来显示提示消息:
from flask import Flask, flash, render_template, request, abortapp = Flask(__name__)app.secret_key = '520'@app.route('/')def index(): flash("Hello loli") return render_template("flash.html")
flash模板:flask开放了get_flashed_messages函数给模板使用,用来得到视图函数中的flash里的字符串(消息)。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><h1>Hello Login</h1><h2>{{ get_flashed_messages()[0] }}</h2></body></html>
这里制作一个简单的表单模拟登陆界面提示:使用request方法得到输入表单中的数据。
@app.route('/login', methods=['POST'])def login(): # 获取表单 form = request.form # 获取表单数据 username = form.get('username') password = form.get('password') # 若不存在username则flash(xxx) if not username: flash('Please input username') return render_template("flash.html") if not password: flash('Please input password') return render_template("flash.html") if username == "loli" and password == "520": flash("Login success") return render_template("flash.html") else: flash("username or password wrong") return render_template('flash.html')
表单模板:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><h1>Hello Login</h1><form action="/login" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="Submit"></form><h2>{{ get_flashed_messages()[0] }}</h2></body></html>
未输入任何数据提示输入username
未输入密码显示的flash提示消息。
用户名和密码不符时。
登陆成功界面。
flask同样可以自己设置404等错误界面:flask提供了errorhandler修饰器来设置自己的错误界面。
@app.errorhandler(404)def not_found(e): return render_template("404.html")
自己设置的简单404错误模板:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <h1>404 页面不存在</h1> <h2>Sorry</h2></body></html>
也可以在正常的界面发生404错误时转到这个模板装饰:用flask import abort方法来引起一个404错误. 只要user_id不为520则触发404页面。
@app.route('/users/<user_id>')def users(user_id): if int(user_id) == 520: return render_template("user.html") else: abort(404)
user模板:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <h1>Loli </h1></body></html>
源码:
#-*- coding:utf-8 -*-from flask import Flask, flash, render_template, request, abortapp = Flask(__name__)app.secret_key = '520'@app.route('/')def index(): flash("Hello loli") return render_template("flash.html")@app.route('/login', methods=['POST'])def login(): # 获取表单 form = request.form # 获取表单数据 username = form.get('username') password = form.get('password') # 若不存在username则flash(xxx) if not username: flash('Please input username') return render_template("flash.html") if not password: flash('Please input password') return render_template("flash.html") if username == "loli" and password == "520": flash("Login success") return render_template("flash.html") else: flash("username or password wrong") return render_template('flash.html')@app.errorhandler(404)def not_found(e): return render_template("404.html")@app.route('/users/<user_id>')def users(user_id): if int(user_id) == 520: return render_template("user.html") else: abort(404)if __name__ == '__main__': app.run()
希望本文所述对大家基于flask框架的Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号