~huyngo/yue

81f400b414116490902126c6facd3864e4143266 — Ngô Ngọc Đức Huy 8 months ago c68d960 python-remake
handle not found errors correctly
2 files changed, 12 insertions(+), 6 deletions(-)

M src/yue/blueprints/pages.py
M src/yue/templates/error/404.html
M src/yue/blueprints/pages.py => src/yue/blueprints/pages.py +10 -5
@@ 1,15 1,20 @@
from http import HTTPStatus
from typing import Optional

import requests
from quart import Blueprint, Response
from quart import Blueprint, abort
from quart.templating import render_template

blueprint = Blueprint('pages', __name__, url_prefix='/page')
STATIC_PAGES = ['about']


@blueprint.route('/<title>', methods=('GET',))
async def static_page(title: str) -> str:
    """Handler for static pages, such as about page or privacy policy."""
    return await render_template([f'pages/{title}.html', 'error/404.html'],
                                 theme='system')
    if title not in STATIC_PAGES:
        abort(HTTPStatus.NOT_FOUND)
    return await render_template(f'pages/{title}.html', theme='system')


@blueprint.errorhandler(HTTPStatus.NOT_FOUND)
async def page_not_found(error):
    return await render_template('error/404.html'), HTTPStatus.NOT_FOUND

M src/yue/templates/error/404.html => src/yue/templates/error/404.html +2 -1
@@ 1,6 1,7 @@
{% extends 'base.html' %}
{% block body %}
<h1>Not found</h1>
<p>
	You cannot sign in without cookies.
	The page you requested does not exist.
</p>
{% endblock %}