zoukankan      html  css  js  c++  java
  • 关于flask

    1.消息提示

    1. 消息提示

    flask 中提供了消息闪现机制,方便我们消息提示,所使用的模块是 flash 模块。在我们使用 flash 时,我们需要调用app.secret_key 字段,该字段会对消息进行加密。具体代码如下:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from flask import Flask, render_template, flash
    
    app = Flask(__name__)
    app.secret_key = '123456'
    
    
    @app.route('/')
    def hello_world():
        flash('hello flash')
        return render_template("index.html")
    
    
    if __name__ == "__main__":
        app.run()

    之后,我们编写 index.html 页面。在该页面中,我们使用 get_flashed_messages() 函数来得到flash中的内容,但注意,这个返回的是一个数组,因为我们只有一条消息,所以我们只取第一个元素,具体代码如下:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
    </head>
    <body>
    <h1>Hello</h1>
    
    <h2>{{ get_flashed_messages()[0] }}</h2>
    </body>
    </html>

    2.异常处理

    在开发时,后台出现异常 ,但不想把异常显示给用户或者要统一处理异常时,可以使用abort主动抛出异常,再捕获异常返回美化后的页面。

    主动抛出异常

    @user.route('/testError')
    def testError():
        print ('testError')
        abort(404)

    使用装饰器errorhandler捕获异常:

    @user.errorhandler(404)
    def error(e):
        return render_template('exception/404.html') // 自己装饰页面 这样就能比较友好的处理 404 错误。

    @app.errorhandler(404) #错误句柄,提示404错误信息
    def not_found1(e):
        title1="error:404"
        header1="Welcome to error:404 page"
        content1="This page went to Mars!"
        #渲染呈现错误信息子页404.html
        return render_template('404.html',title=title1, header=header1, content=content1)
    
    @app.errorhandler(500) #错误句柄,提示500错误信息
    def not_found2(e):
        title1="error:500"
        header1="Welcome to error:500 page"
        content1="This page has been aborted!"
        #渲染呈现错误信息子页500.html
        return render_template('500.html',title=title1, header=header1, content=content1)

    对于异常,通常可以分为两类:一类是可以预知的异常,我们通常会用try...except....捕捉,第二类是未知的error,我们是无法预知的。

        try:

          code block

        except A:

          except A handle

        except:

          other except

        else:

          if no exception, excute here

        finally:

          code

        对于try....except捕捉异常,如果在try中发生了异常,会在except中捕捉到,并处理。如果没有发生异常,会执行else语句

        但是不论异常是否发生,都会执行finally语句,所以我们一般会在finally语句中释放资源。

    下面是 Python 异常处理机制的语法结构:

    try:
        #业务实现代码
        ...
    except (Error1, Error2, ...) as e:
        alert 输入不合法
        goto retry

    如果在执行 try 块里的业务逻辑代码时出现异常,系统自动生成一个异常对象,该异常对象被提交给 Python 解释器,这个过程被称为引发异常。

    当 Python 解释器收到异常对象时,会寻找能处理该异常对象的 except 块,如果找到合适的 except 块,则把该异常对象交给该 except 块处理,这个过程被称为捕获异常。如果 Python 解释器找不到捕获异常的 except 块,则运行时环境终止,Python 解释器也将退出。

    try:
        f = open('temp.txt','w')
        print(f.write('到底写进去了没有呢?'))
        sum = 1 + '1'
    except OSError as reason:
        print('不存在这个文件' + str(reason))
    except TypeError as reason:
        print('类型错误' + str(reason))
    finally:
        f.close()  #如果不关闭的话,就没有写进去
  • 相关阅读:
    Java开发web的几种开发模式
    Tomcat7.0安装配置
    Apache与Tomcat 区别联系
    编写 Window 服务程序
    系列文章--SharePoint 开发教程
    Google Chrome浏览器调试功能介绍
    Chrome的JS调试工具
    Google Chrome 调试JS简单教程[更新]
    ASP.NET常用标准配置web.config
    WINDOWS下kill进程的命令
  • 原文地址:https://www.cnblogs.com/yundong333/p/11061786.html
Copyright © 2011-2022 走看看