我在__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)
答案 0 :(得分:16)
我没有意识到的东西......来自Flask docs
请注意,如果为“500 Internal Server”添加错误处理程序 错误“,如果它在调试模式下运行,Flask将不会触发它。