python - Flask error handler -
i created simple multi-application flask site. moved errors view (404, 501, etc.) application errors
. when error occures see default error pages of flask, not own. structure of project is:
/site |-> main.py flask import flask app = flask(__name__) app.config.from_pyfile('config.py') import errors, account, mysite, blog errors.views import not_found @app.route("/") def index_view(): return "welcome!" @app.route("/about") def about_view(): return "about" if __name__ == "__main__": app.run(debug=true) |-> templates |-> errors |-> 404.html |->errors |-> __init__.py main import app import errors.views |-> views errors import app flask import render_template @app.errorhandler(404) def not_found(error): return render_template('errors/404.html'), 404
if returning content of errors/views.py
main.py
begins work expected , show me error page.
put error templates 404 etc. in main templates folder of project. if project called "myproject", can have like:
myproject/ __init__.py templates 404.html 503.html .. , on
call these in __init__.py
file as:
app = flask(__name__) @app.errorhandler(404) def not_found(error): return render_template('404.html'), 404
Comments
Post a Comment