python - Flask custom error page 500 not working -
i have following code in __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
it used catch 500 errors , show nice 500.html template. moved views separate blueprint files , 500 errorhandler not work. handler though. 404 works fine.
if server throws 500 error, display default chrome internal server error message , not template. did wrong when created blueprints create issue?
here entire __init__.py
file
import datetime import mysql.connector import os flask import flask, render_template, session, request, blueprint flask.ext.moment import moment flask.ext.login import loginmanager db_classes import user info import info_blueprint claims import claims_blueprint users import users_blueprint members import members_blueprint drug import drug_blueprint auth import auth_blueprint formulary import formulary_blueprint config import mysql_usr, mysql_host, mysql_pass, mysql_db, mysql_port, second_to_live 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)
something didn't realize... flask docs
please note if add error handler “500 internal server error”, flask not trigger if it’s running in debug mode.
Comments
Post a Comment