Skip to content
Myr

Merge pull request #885 from jmhobbs/master

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

Update url_for documentation for Flask.url_build_error_handlers

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 21f3ad2, e31a2a8 · Browse files · 1 file+7 added−7 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+7 added−7 removed

  • other:+7 added−7 removed

1 file+7 added−7 removed

modified:flask/helpers.py+7 added-7 removed
197 unmodified lines
198199 For more information, head over to the :ref:`Quickstart <url-building>`.200201 To integrate applications, :class:`Flask` has a hook to intercept URL build202 errors through :attr:`Flask.build_error_handler`. The `url_for` function203 results in a :exc:`~werkzeug.routing.BuildError` when the current app does204 not have a URL for the given endpoint and values. When it does, the205 :data:`~flask.current_app` calls its :attr:`~Flask.build_error_handler` if202 errors through :attr:`Flask.url_build_error_handlers`. The `url_for`203 function results in a :exc:`~werkzeug.routing.BuildError` when the current204 app does not have a URL for the given endpoint and values. When it does, the205 :data:`~flask.current_app` calls its :attr:`~Flask.url_build_error_handlers` if206 it is not `None`, which can return a string to use as the result of207 `url_for` (instead of `url_for`'s default to raise the208 :exc:`~werkzeug.routing.BuildError` exception) or re-raise the exception.209 An example::210211 def external_url_handler(error, endpoint, **values):211 def external_url_handler(error, endpoint, values):212 "Looks up an external URL when `url_for` cannot build a URL."213 # This is an example of hooking the build_error_handler.214 # Here, lookup_url is some utility function you've built215 # which looks up the endpoint in some external URL registry.
8 unmodified lines
224 raise error225 # url_for will use this result, instead of raising BuildError.226 return url227228 app.build_error_handler = external_url_handler228 app.url_build_error_handlers.append(external_url_handler)229230 Here, `error` is the instance of :exc:`~werkzeug.routing.BuildError`, and231 `endpoint` and `**values` are the arguments passed into `url_for`. Note231 `endpoint` and `values` are the arguments passed into `url_for`. Note232 that this is for building URLs outside the current application, and not for233 handling 404 NotFound errors.234235 .. versionadded:: 0.10
626 unmodified lines