Registered error handler in Python/Flask application does not work

from flask import jsonify, render_template

# at the application level
# not the blueprint level
def page_not_found(e):
    # if a request is in our blog URL space
    if request.path.startswith('/blog/'):
        # we return a custom blog 404 page
        return render_template("blog/404.html"), 404
        # otherwise we return our generic site-wide 404 page
        return render_template("404.html"), 404

def method_not_allowed(e):
    # if a request has the wrong method to our API
    if request.path.startswith('/api/'):
        # we return a json saying so
        return jsonify(message="Method Not Allowed"), 405
        # otherwise we return a generic site-wide 405 page
        return render_template("405.html"), 405