【问题标题】:Flask custom error page 500 not working烧瓶自定义错误页面500不起作用
【发布时间】:2015-11-09 10:51:21
【问题描述】:

我在__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 工作得很好。

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

这是整个__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)

【问题讨论】:

  • __init__.py 是否被您的应用程序入口点导入,最好是在您创建应用程序时导入?
  • 这是我用来启动应用程序的文件,所以我想是的。创建应用程序代码仅在错误处理程序上方几行。就像我说的那样,它一直在工作,直到我通过蓝图将所有视图移到单独的文件中。我在上面发布了整个文件。

标签: python error-handling flask


【解决方案1】:

我没有意识到的事情......来自 Flask docs

请注意,如果您为“500 内部服务器”添加错误处理程序 错误”,如果 Flask 在 Debug 模式下运行,则不会触发它。

【讨论】:

  • 是的,解决了这个问题。我之前一直使用if __name__ == "__main__": app.run(host= '0.0.0.0', debug=True) 运行该应用程序。当您通过 gunicorn 执行此操作时,它不会使用该调试值。当我重构使用配置文件时,gunicorn 从配置文件中提取了 debug=True 并且 500 模板停止工作。感谢您提供此信息。
  • 链接已损坏。有机会请及时更新。
  • 看起来在 Flask 1.1 中实际上不再是这种情况,至少,我再也找不到对这种行为的任何引用。自 1.0 之前的版本以来,Flask 发生了很大变化,尤其是在 CLI 和入口点方面。
  • 在 1.1.2 中仍然遇到这种情况。
猜你喜欢
  • 2015-03-08
  • 2020-08-12
  • 1970-01-01
  • 2015-08-26
  • 2013-03-06
  • 2012-07-24
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多