烧瓶自定义错误页面500不起作用 [英] Flask custom error page 500 not working

查看:71
本文介绍了烧瓶自定义错误页面500不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 __ init __。py

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

@app.errorhandler(403)
def page_forbidden(e):
    return render_template('403.html'), 500

过去用来捕获所有500个错误并显示我的好的500.html模板。但是,我将所有视图移到了单独的蓝图文件中,现在500错误处理程序不起作用。虽然只有那个处理程序。 404正常工作。

It used to catch all 500 errors and show my nice 500.html template. However I moved all my views into separate blueprint files and now the 500 errorhandler does not work. It is only that handler though. 404 works just fine.

如果服务器抛出500错误,它将显示默认的Chrome内部服务器错误消息,而不是我的模板。创建所有会导致此问题的蓝图时,我做错什么了吗?

If the server throws a 500 error, it will display the default Chrome INTERNAL SERVER ERROR message and not my template. Did I do something wrong when I created all my blueprints that would create this issue?

这是整个 __ init __。py 文件

import datetime
import mysql.connector
import os
from flask import Flask, render_template, session, request, Blueprint
from flask.ext.moment import Moment
from flask.ext.login import LoginManager
from db_classes import User

from info import info_blueprint
from claims import claims_blueprint
from users import users_blueprint
from members import members_blueprint
from drug import drug_blueprint
from auth import auth_blueprint
from formulary import formulary_blueprint

from config import MYSQL_USR, MYSQL_HOST, MYSQL_PASS, MYSQL_DB, MYSQL_PORT, second_to_live

from decorators import role_required

app = Flask(__name__, template_folder="static/templates")
app.config.from_object('config')

moment = Moment(app)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.session_protection = 'strong'
login_manager.login_view = 'login'

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


####################
#   Blueprints
####################

app.register_blueprint(info_blueprint)
app.register_blueprint(claims_blueprint)
app.register_blueprint(users_blueprint)
app.register_blueprint(members_blueprint)
app.register_blueprint(drug_blueprint)
app.register_blueprint(formulary_blueprint)
app.register_blueprint(auth_blueprint)



#####################
#   Error Routes
#####################  


@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'), 500

@app.errorhandler(403)
def page_forbidden(e):
    return render_template('403.html'), 500

#####################
#   Context Processors
#####################

@app.before_request
def make_session_permanent():
    session.permanent = True
    app.permanent_session_lifetime = datetime.timedelta(seconds=second_to_live)

@app.context_processor
def inject_time():
    return dict(current_time=datetime.datetime.utcnow())

if __name__ == "__main__":
    app.run(host= '0.0.0.0', debug=True)


推荐答案

从Flask中我没意识到...... 文档

Something I didn't realize... from the Flask docs


请注意,如果您为 500添加了错误处理程序内部服务器
错误,如果Flask以调试模式运行,则Flask不会触发它。

Please note that if you add an error handler for "500 Internal Server Error", Flask will not trigger it if it’s running in Debug mode.

这篇关于烧瓶自定义错误页面500不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆