Flask框架学习笔记之消息提示与异常处理操作详解

yipeiwu_com7年前Python基础

本文实例讲述了Flask框架学习笔记之消息提示与异常处理操作。分享给大家供大家参考,具体如下:

flask通过flash方法来显示提示消息:

from flask import Flask, flash, render_template, request, abort

app = 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, abort

app = 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程序设计有所帮助。

相关文章

Python3实现将文件归档到zip文件及从zip文件中读取数据的方法

本文实例讲述了Python3实现将文件归档到zip文件及从zip文件中读取数据的方法。分享给大家供大家参考。具体实现方法如下: ''''' Created on Dec 24, 2...

在python中用url_for构造URL的方法

用url_for构造URL,他接受函数名作为第一个参数,也接受对应URL规则的变量部分的命名参数,未知的变量部分会添加到URL末尾作为查询参数。 构建URL而不选择直接在代码中拼URL的...

深入了解Django View(视图系统)

深入了解Django View(视图系统)

Django View 官方文档 一个视图函数(类),简称视图,是一个简单的 Python 函数(类),它接受Web请求并且返回Web响应。响应可以是一张网页的HTML内容,一个重定向...

Python cookbook(数据结构与算法)实现对不原生支持比较操作的对象排序算法示例

本文实例讲述了Python实现对不原生支持比较操作的对象排序算法。分享给大家供大家参考,具体如下: 问题:想在同一个类的实例之间做排序,但是它们并不原生支持比较操作。 解决方案:使用内建...

深入浅析python3中的unicode和bytes问题

最近写了一些python3程序,四处能看到bytes类型,而它并不存在于python2中,这也是python3和python2显著区别之一。 以前在写python2代码的时候,经常会遇到...