Added pattern for url processors because the API documentation itself might not reveal much.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 8617505 · Browse files · 2 files·+127 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+127 added−0 removed
- other:+127 added−0 removed
Provenance
- Commit
d62e277- Tree
c61ae79- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
8617505
2 files changed+127 added−0 removed
modified:docs/patterns/index.rst+1 added
17 unmodified lines18 19 packages20 appfactories21 appdispatch22+ urlprocessors23 distribute24 fabric25 sqlite326 sqlalchemy11 unmodified linesadded:docs/patterns/urlprocessors.rst+126 added
1+Using URL Processors2+====================3+4+.. versionadded:: 0.75+6+Flask 0.7 introduces the concept of URL processors. The idea is that you7+might have a bunch of resources with common parts in the URL that you8+don't always explicitly want to provide. For instance you might have a9+bunch of URLs that have the language code in it but you don't want to have10+to handle it in every single function yourself.11+12+URL processors are especially helpful when combined with blueprints. We13+will handle both application specific URL processors here as well as14+blueprint specifics.15+16+Internationalized Application URLs17+----------------------------------18+19+Consider an application like this::20+21+ from flask import Flask, g22+23+ app = Flask(__name__)24+25+ @app.route('/<lang_code>/')26+ def index(lang_code):27+ g.lang_code = lang_code28+ ...29+30+ @app.route('/<lang_code>/about')31+ def about(lang_code):32+ g.lang_code = lang_code33+ ...34+35+This is an awful lot of reptition as you have to handle the language code36+setting on the :data:`~flask.g` object yourself in every single function.37+Sure, a decorator could be used to simplify this, but if you want to38+generate URLs from one function to another you would have to still provide39+the language code explicitly which can be annoying.40+41+For the latter, this is where :func:`~flask.Flask.url_defaults` functions42+come in. They can automatically inject values into a call for43+:func:`~flask.url_for` automatically. The code below checks if the44+language code is not yet in the dictionary of URL values and if the45+endpoint wants a value named ``'lang_code'``::46+47+ @app.url_defaults48+ def add_language_code(endpoint, values):49+ if 'lang_code' in values or not g.lang_code:50+ return51+ if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):52+ values['lang_code'] = g.lang_code53+54+The method :meth:`~werkzeug.routing.Map.is_endpoint_expecting` of the URL55+map can be used to figure out if it would make sense to provide a language56+code for the given endpoint.57+58+The reverse of that function are59+:meth:`~flask.Flask.url_value_preprocessor`\s. They are executed right60+after the request was matched and can execute code based on the URL61+values. The idea is that they pull information out of the values62+dictionary and put it somewhere else::63+64+ @app.url_value_preprocessor65+ def pull_lang_code(endpoint, values):66+ g.lang_code = values.pop('lang_code', None)67+68+That way you no longer have to do the `lang_code` assigment to69+:data:`~flask.g` in every function. You can further improve that by70+writing your own decorator that prefixes URLs with the language code, but71+the more beautiful solution is using a blueprint. Once the72+``'lang_code'`` is popped from the values dictionary and it will no longer73+be forwarded to the view function reducing the code to this::74+75+ from flask import Flask, g76+77+ app = Flask(__name__)78+79+ @app.url_defaults80+ def add_language_code(endpoint, values):81+ if 'lang_code' in values or not g.lang_code:82+ return83+ if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):84+ values['lang_code'] = g.lang_code85+86+ @app.url_value_preprocessor87+ def pull_lang_code(endpoint, values):88+ g.lang_code = values.pop('lang_code', None)89+90+ @app.route('/<lang_code>/')91+ def index():92+ ...93+94+ @app.route('/<lang_code>/about')95+ def about():96+ ...97+98+Internationalized Blueprint URLs99+--------------------------------100+101+Because blueprints can automatically prefix all URLs with a common string102+it's easy to automatically do that for every function. Furthermore103+blueprints can have per-blueprint URL processors which removes a whole lot104+of logic from the :meth:`~flask.Flask.url_defaults` function because it no105+longer has to check if the URL is really interested in a ``'lang_code'``106+parameter::107+108+ from flask import Blueprint, g109+110+ bp = Blueprint('frontend', __name__, url_prefix='/<lang_code>')111+112+ @bp.url_defaults113+ def add_language_code(endpoint, values):114+ values.setdefault('lang_code', g.lang_code)115+116+ @bp.url_value_preprocessor117+ def pull_lang_code(endpoint, values):118+ g.lang_code = values.pop('lang_code')119+120+ @bp.route('/')121+ def index():122+ ...123+124+ @bp.route('/about')125+ def about():126+ ...