diff --git a/app.py b/app.py index 488ea463..718190bb 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,7 @@ import os -from flask import Flask, abort, send_from_directory +from flask import Flask, abort, send_from_directory, render_template from flask_sslify import SSLify from flask_seasurf import SeaSurf from flask_talisman import Talisman @@ -39,7 +39,7 @@ def find_key(token): "frame-ancestors": "'none'", "object-src": "'none'", } -app = Flask(__name__) +app = Flask(__name__, template_folder=ROOT) app.config["SECRET_KEY"] = os.urandom(16) app.config["SESSION_COOKIE_NAME"] = "__Secure-session" app.config["SESSION_COOKIE_SAMESITE"] = "Strict" @@ -53,6 +53,12 @@ def find_key(token): sslify = SSLify(app, skips=[".well-known"]) +@app.errorhandler(404) +def page_not_found(e): + """Redirect to 404.html.""" + return render_template("404.html"), 404 + + @app.after_request def add_feature_policy(response): """Add feature policy.""" diff --git a/app_test.py b/app_test.py index ea4dad2f..0861e85d 100644 --- a/app_test.py +++ b/app_test.py @@ -13,6 +13,7 @@ static_proxy, index_redirection, add_feature_policy, + page_not_found, ) from app import ROOT @@ -139,6 +140,11 @@ def test_static_proxy(self): self.assertEqual(resp.status_code, 200) resp.close() + def test_page_not_found(self): + """Test page not found.""" + html, status_code = page_not_found(None) + self.assertEqual(status_code, 404) + if __name__ == "__main__": unittest.main() diff --git a/docs/404.rst b/docs/404.rst new file mode 100644 index 00000000..57e66948 --- /dev/null +++ b/docs/404.rst @@ -0,0 +1,8 @@ +:orphan: + +404 Page Not Found +================== + +What you were looking for is just not there. + +`Click here to go back to homepage. `_ diff --git a/docs/_templates/layout.html b/docs/_templates/layout.html index 1e3acf05..5cf00693 100644 --- a/docs/_templates/layout.html +++ b/docs/_templates/layout.html @@ -22,5 +22,14 @@ gtag('config', '{{ tracking_id }}'); {% endif -%} +{%- if pagename == '404' -%} + +{%- endif -%} {% endblock %} {% set css_files = css_files + [ "_static/style.css" ] %}