Skip to content
Myr

Config is now available in templates, context processors no longer override keys

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 ed16ae2 · Browse files · 5 files+47 added−1 removed
This commit

State

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

Diff stats

5 files+47 added−1 removed

  • tests:+24 added−0 removed
  • other:+23 added−1 removed

5 files+47 added−1 removed

modified:docs/upgrading.rst+8 added
28 unmodified lines
29how other systems handle request pre- and postprocessing. If you30dependend on the order of execution of post-request functions, be sure to31change the order.3233Another change that breaks backwards compatibility is that context34processors will no longer override values passed directly to the template35rendering function. If for example `request` is as variable passed36directly to the template, the default context processor will not override37it with the current request object. This makes it easier to extend38context processors later to inject additional variables without breaking39existing template not expecting them.4041Version 0.542-----------4344Flask 0.5 is the first release that comes as a Python package instead of a
68 unmodified lines
modified:flask/app.py+10 added-1 removed
341 unmodified lines
342 return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))343344 def update_template_context(self, context):345 """Update the template context with some commonly used variables.346 This injects request, session and g into the template context.346 This injects request, session, config and g into the template347 context as well as everything template context processors want348 to inject. Note that the as of Flask 0.6, the original values349 in the context will not be overriden if a context processor350 decides to return a value with the same key.351352 :param context: the context as a dictionary that is updated in place353 to add extra variables.354 """355 funcs = self.template_context_processors[None]356 mod = _request_ctx_stack.top.request.module357 if mod is not None and mod in self.template_context_processors:358 funcs = chain(funcs, self.template_context_processors[mod])359 orig_ctx = context.copy()360 for func in funcs:361 context.update(func())362 # make sure the original values win. This makes it possible to363 # easier add new variables in context processors without breaking364 # existing views.365 context.update(orig_ctx)366367 def run(self, host='127.0.0.1', port=5000, **options):368 """Runs the application on a local development server. If the369 :attr:`debug` flag is set the server will automatically reload
487 unmodified lines
modified:flask/templating.py+1 added
17 unmodified lines
18 `session` and `g`.19 """20 reqctx = _request_ctx_stack.top21 return dict(22 config=reqctx.app.config,23 request=reqctx.request,24 session=reqctx.session,25 g=reqctx.g26 )
63 unmodified lines
modified:tests/flask_tests.py+24 added
490 unmodified lines
491 return flask.render_template('context_template.html', value=23)492 rv = app.test_client().get('/')493 assert rv.data == '<p>23|42'494495 def test_original_win(self):496 app = flask.Flask(__name__)497 @app.route('/')498 def index():499 return flask.render_template_string('{{ config }}', config=42)500 rv = app.test_client().get('/')501 assert rv.data == '42'502503 def test_standard_context(self):504 app = flask.Flask(__name__)505 app.secret_key = 'development key'506 @app.route('/')507 def index():508 flask.g.foo = 23509 flask.session['test'] = 'aha'510 return flask.render_template_string('''511 {{ request.args.foo }}512 {{ g.foo }}513 {{ config.DEBUG }}514 {{ session.test }}515 ''')516 rv = app.test_client().get('/?foo=42')517 assert rv.data.split() == ['42', '23', 'False', 'aha']518519 def test_escaping(self):520 text = '<p>Hello World!'521 app = flask.Flask(__name__)522 @app.route('/')
495 unmodified lines
modified:CHANGES+4 added
15 unmodified lines
16- static rules are now even in place if there is no static folder17 for the module. This was implemented to aid GAE which will18 remove the static folder if it's part of a mapping in the .yml19 file.20- the :attr:`~flask.Flask.config` is now available in the templates21 as `config`.22- context processors will no longer override values passed directly23 to the render function.2425Version 0.5.126-------------27
98 unmodified lines