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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Python使用统计函数绘制简单图形实例代码

Python使用统计函数绘制简单图形实例代码

前言 Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。 它也可以和图形工具包一起使用,如 PyQt 和 w...

python字典键值对的添加和遍历方法

添加键值对 首先定义一个空字典 >>> dic={} 直接对字典中不存在的key进行赋值来添加 >>> dic['name']='zhangsan...

python3.5使用tkinter制作记事本

tkinter是Python下面向tk的图形界面接口库,可以方便地进行图形界面设计和交互操作编程。tkinter的优点是简单易用、与Python的结合度好。tkinter在Python...

python基础之包的导入和__init__.py的介绍

调用同级目录: – src |– mod.py |– test.py 若在程序test.py中导入模块mod, 则直接使用 import mod 或 from mod im...

python中 logging的使用详解

日志是用来记录程序在运行过程中发生的状况,在程序开发过程中添加日志模块能够帮助我们了解程序运行过程中发生了哪些事件,这些事件也有轻重之分。 根据事件的轻重可分为以下几个级别: DEBUG...