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
Provenance
- Commit
0c45976- Tree
2f4343e- Author
John Still <john@jmsdvl.com>- Committer
John Still <john@jmsdvl.com>- Parent
67e3919
1 file changed+23 added−5 removed
modified:docs/blueprints.rst+23 added-5 removed
175 unmodified lines176 177 admin = Blueprint('admin', __name__, template_folder='templates')178 179 For static files, the path can be absolute or relative to the blueprint180-resource folder. 180+resource folder.181 182-The template folder is added to the search path of templates but with a lower 183-priority than the actual application's template folder. That way you can 184-easily override templates that a blueprint provides in the actual application. 182+The template folder is added to the search path of templates but with a lower183+priority than the actual application's template folder. That way you can184+easily override templates that a blueprint provides in the actual application.185 This also means that if you don't want a blueprint template to be accidentally186 overridden, make sure that no other blueprint or actual application template187 has the same relative path. When multiple blueprints provide the same relative188 template path the first blueprint registered takes precedence over the others.4 unmodified lines193 ``templates`` as a `template_folder` you will have to create a file like194 this: :file:`yourapplication/admin/templates/admin/index.html`. The reason195 for the extra ``admin`` folder is to avoid getting our template overridden196 by a template named ``index.html`` in the actual application template197-folder. 197+folder.198 199 To further reiterate this: if you have a blueprint named ``admin`` and you200 want to render a template called :file:`index.html` which is specific to this201 blueprint, the best idea is to lay out your templates like this::42 unmodified lines244 @simple_page.errorhandler(404)245 def page_not_found(e):246 return render_template('pages/404.html')247 248+Most errorhandlers will simply work as expected; however, there is a caveat249+concerning handlers for 404 and 405 exceptions. These errorhandlers are only250+invoked from an appropriate ``raise`` statement or a call to ``abort`` in another251+of the blueprint's view functions; they are not invoked by, e.g., an invalid URL252+access. This is because the blueprint does not "own" a certain URL space, so253+the application instance has no way of knowing which blueprint errorhandler it254+should run if given an invalid URL. If you would like to execute different255+handling strategies for these errors based on URL prefixes, they may be defined256+at the application level using the ``request`` proxy object::257+258+ @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 ex265+266 More information on error handling see :ref:`errorpages`.