Skip to content
Myr

Merge pull request #1868 from antsar/master

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

Rewrite subclassing example with a better use-case.

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 8458cc5, 447f591 · Browse files · 1 file+8 added−13 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+8 added−13 removed

  • other:+8 added−13 removed

1 file+8 added−13 removed

modified:docs/patterns/subclassing.rst+8 added-13 removed
1 unmodified line
2=================34The :class:`~flask.Flask` class is designed for subclassing.56One reason to subclass would be customizing the Jinja2 :class:`~jinja2.Environment`. For example, to add a new global template variable::6For example, you may want to override how request parameters are handled to preserve their order::78 from flask import Flask9 from datetime import datetime108 from flask import Flask, Request9 from werkzeug.datastructures import ImmutableOrderedMultiDict10 class MyRequest(Request):11 """Request subclass to override request parameter storage"""12 parameter_storage_class = ImmutableOrderedMultiDict13 class MyFlask(Flask):12 """ Flask with more global template vars """14 """Flask subclass using the custom request class"""15 request_class = MyRequest1614 def create_jinja_environment(self):15 """ Initialize my custom Jinja environment. """16 jinja_env = super(MyFlask, self).create_jinja_environment(self)17 jinja_env.globals.update(18 current_time = datetime.datetime.now()19 )20 return jinja_env2117This is the recommended approach for overriding or augmenting Flask's internal functionality.