Skip to content
Myr

clarify blueprint 404 error handling in docs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John Still · committed by John Still · parent 67e3919 · Browse files · 1 file+23 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+23 added−5 removed

  • other:+23 added−5 removed

1 file+23 added−5 removed

modified:docs/blueprints.rst+23 added-5 removed
175 unmodified lines
176177 admin = Blueprint('admin', __name__, template_folder='templates')178179For static files, the path can be absolute or relative to the blueprint180resource folder. 180resource folder.181182The template folder is added to the search path of templates but with a lower 183priority than the actual application's template folder. That way you can 184easily override templates that a blueprint provides in the actual application. 182The template folder is added to the search path of templates but with a lower183priority than the actual application's template folder. That way you can184easily override templates that a blueprint provides in the actual application.185This also means that if you don't want a blueprint template to be accidentally186overridden, make sure that no other blueprint or actual application template187has the same relative path. When multiple blueprints provide the same relative188template path the first blueprint registered takes precedence over the others.
4 unmodified lines
193``templates`` as a `template_folder` you will have to create a file like194this: :file:`yourapplication/admin/templates/admin/index.html`. The reason195for the extra ``admin`` folder is to avoid getting our template overridden196by a template named ``index.html`` in the actual application template197folder. 197folder.198199To further reiterate this: if you have a blueprint named ``admin`` and you200want to render a template called :file:`index.html` which is specific to this201blueprint, the best idea is to lay out your templates like this::
42 unmodified lines
244 @simple_page.errorhandler(404)245 def page_not_found(e):246 return render_template('pages/404.html')247248Most errorhandlers will simply work as expected; however, there is a caveat249concerning handlers for 404 and 405 exceptions. These errorhandlers are only250invoked from an appropriate ``raise`` statement or a call to ``abort`` in another251of the blueprint's view functions; they are not invoked by, e.g., an invalid URL252access. This is because the blueprint does not "own" a certain URL space, so253the application instance has no way of knowing which blueprint errorhandler it254should run if given an invalid URL. If you would like to execute different255handling strategies for these errors based on URL prefixes, they may be defined256at the application level using the ``request`` proxy object::257258 @app.errorhandler(404)259 @app.errorhandler(405)260 def _handle_api_error(ex):261 if request.path.startswith('/api/'):262 return jsonify_error(ex)263 else:264 return ex265266More information on error handling see :ref:`errorpages`.