Flask -- 消息闪现、错误处理


 flash 可以在任何需要的地方添加,类似于print

from flask import flash


@app.route('/')
def index():
    flash('You are in home page now')
    return render_template('index.html')
#index.html


{% for message in get_flashed_messages() %}    # 和session一样, 可以在模板中直接使用

    {{ message }}

{% endfor %}

自定义错误页面

@app.errorhandler(404)
def page_not_found(error):
    try:
        return render_template('404.html')
    except Exception as e:
        return render_template('500.html', error=e)
KEEP LEARNING!
原文地址:https://www.cnblogs.com/roronoa-sqd/p/5440561.html