"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "docs/patterns/urlprocessors.rst" between
flask-1.1.1.tar.gz and flask-1.1.2.tar.gz

About: Flask is a lightweight Python WSGI web application framework.

urlprocessors.rst  (flask-1.1.1):urlprocessors.rst  (flask-1.1.2)
skipping to change at line 42 skipping to change at line 42
g.lang_code = lang_code g.lang_code = lang_code
... ...
This is an awful lot of repetition as you have to handle the language code This is an awful lot of repetition as you have to handle the language code
setting on the :data:`~flask.g` object yourself in every single function. setting on the :data:`~flask.g` object yourself in every single function.
Sure, a decorator could be used to simplify this, but if you want to Sure, a decorator could be used to simplify this, but if you want to
generate URLs from one function to another you would have to still provide generate URLs from one function to another you would have to still provide
the language code explicitly which can be annoying. the language code explicitly which can be annoying.
For the latter, this is where :func:`~flask.Flask.url_defaults` functions For the latter, this is where :func:`~flask.Flask.url_defaults` functions
come in. They can automatically inject values into a call for come in. They can automatically inject values into a call to
:func:`~flask.url_for` automatically. The code below checks if the :func:`~flask.url_for`. The code below checks if the
language code is not yet in the dictionary of URL values and if the language code is not yet in the dictionary of URL values and if the
endpoint wants a value named ``'lang_code'``:: endpoint wants a value named ``'lang_code'``::
@app.url_defaults @app.url_defaults
def add_language_code(endpoint, values): def add_language_code(endpoint, values):
if 'lang_code' in values or not g.lang_code: if 'lang_code' in values or not g.lang_code:
return return
if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'): if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
values['lang_code'] = g.lang_code values['lang_code'] = g.lang_code
 End of changes. 1 change blocks. 
2 lines changed or deleted 2 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)