From 7bc8516d09b4b8306454262366866eb3db2641f0 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Tue, 9 Oct 2018 22:36:32 +0200 Subject: [PATCH] Chapter 3: Custom error pages (3c) --- hello.py | 14 ++++++++++++-- templates/404.html | 9 +++++++++ templates/500.html | 9 +++++++++ templates/base.html | 31 +++++++++++++++++++++++++++++++ templates/index.html | 10 +++++++++- templates/user.html | 32 ++++---------------------------- 6 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 templates/404.html create mode 100644 templates/500.html create mode 100644 templates/base.html diff --git a/hello.py b/hello.py index 62ab3a3..83940c1 100644 --- a/hello.py +++ b/hello.py @@ -6,11 +6,21 @@ app = Flask(__name__) bootstrap = Bootstrap(app) +@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.route('/') def index(): - return render_template('index.html') + return render_template('index.html') @app.route('/user/') def user(name): - return render_template('user.html', name=name) + return render_template('user.html', name=name) diff --git a/templates/404.html b/templates/404.html new file mode 100644 index 0000000..32b50c6 --- /dev/null +++ b/templates/404.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block title %}Flasky - Page not found{% endblock %} + +{% block page_content %} + +{% endblock %} \ No newline at end of file diff --git a/templates/500.html b/templates/500.html new file mode 100644 index 0000000..517f21a --- /dev/null +++ b/templates/500.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block title %}Flasky - Internal server error{% endblock %} + +{% block page_content %} + +{% endblock %} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..fab11b8 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,31 @@ +{% extends "bootstrap/base.html" %} + +{% block title %}Flasky{% endblock %} + +{% block navbar %} + +{% endblock %} + +{% block content %} +
+ {% block page_content %}{% endblock %} +
+{% endblock %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index ba7c290..122059b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1 +1,9 @@ -

Hello World!

\ No newline at end of file +{% extends "base.html" %} + +{% block title %}Flasky{% endblock %} + +{% block page_content %} + +{% endblock %} \ No newline at end of file diff --git a/templates/user.html b/templates/user.html index 6bb1462..5fe92ee 100644 --- a/templates/user.html +++ b/templates/user.html @@ -1,33 +1,9 @@ -{% extends "bootstrap/base.html" %} +{% extends "base.html" %} {% block title %}Flasky{% endblock %} -{% block navbar %} -