Skip to content
Myr

docs: ``with``, ``for``, ``self``

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

authored by defuz · committed by defuz · parent 0bb4de1 · Browse files · 10 files+24 added−24 removed
This commit

State

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

Diff stats

10 files+24 added−24 removed

  • other:+24 added−24 removed

10 files+24 added−24 removed

modified:docs/patterns/fabric.rst+3 added-3 removed
70 unmodified lines
71- `run` - executes a command on a remote server72- `local` - executes a command on the local machine73- `put` - uploads a file to the remote server74- `cd` - changes the directory on the serverside. This has to be used75 in combination with the `with` statement.75 in combination with the ``with`` statement.7677Running Fabfiles78----------------79
33 unmodified lines
113 Make sure to activate watching for changes of the `.wsgi` file so114 that we can automatically reload the application by touching it.115 (See :ref:`mod_wsgi-deployment` for more information)116117So now the question is, where do the `application.wsgi` and118`application.cfg` files come from?117So now the question is, where do the :file:`application.wsgi` and118:file:`application.cfg` files come from?119120The WSGI File121-------------122
74 unmodified lines
modified:docs/tutorial/templates.rst+1 added-1 removed
54 unmodified lines
55show_entries.html56-----------------5758This template extends the :file:`layout.html` template from above to display the59messages. Note that the `for` loop iterates over the messages we passed59messages. Note that the ``for`` loop iterates over the messages we passed60in with the :func:`~flask.render_template` function. We also tell the61form to submit to your `add_entry` function and use ``POST`` as HTTP62method:63
48 unmodified lines
modified:docs/extensiondev.rst+1 added-1 removed
257 unmodified lines
258 with app.app_context():259 cur = db.connection.cursor()260 cur.execute(...)261262At the end of the `with` block the teardown handles will be executed262At the end of the ``with`` block the teardown handles will be executed263automatically.264265Additionally, the ``init_app`` method is used to support the factory pattern266for creating apps::
154 unmodified lines
modified:docs/quickstart.rst+1 added-1 removed
508 unmodified lines
509will notice that code which depends on a request object will suddenly break510because there is no request object. The solution is creating a request511object yourself and binding it to the context. The easiest solution for512unit testing is to use the :meth:`~flask.Flask.test_request_context`513context manager. In combination with the `with` statement it will bind a513context manager. In combination with the ``with`` statement it will bind a514test request so that you can interact with it. Here is an example::515516 from flask import request517
376 unmodified lines
modified:docs/reqcontext.rst+3 added-3 removed
35 unmodified lines
36us a :class:`~flask.ctx.RequestContext`:3738>>> ctx = app.test_request_context('/?next=http://example.com/')3940This context can be used in two ways. Either with the `with` statement40This context can be used in two ways. Either with the ``with`` statement41or by calling the :meth:`~flask.ctx.RequestContext.push` and42:meth:`~flask.ctx.RequestContext.pop` methods:4344>>> ctx.push()
29 unmodified lines
74 return response(environ, start_response)7576The method :meth:`~Flask.request_context` returns a new77:class:`~flask.ctx.RequestContext` object and uses it in combination with78the `with` statement to bind the context. Everything that is called from79the same thread from this point onwards until the end of the `with`78the ``with`` statement to bind the context. Everything that is called from79the same thread from this point onwards until the end of the ``with``80statement will have access to the request globals (:data:`flask.request`81and others).8283The request context internally works like a stack: The topmost level on
140 unmodified lines
modified:docs/shell.rst+1 added-1 removed
43 unmodified lines
44us a :class:`~flask.ctx.RequestContext`:4546>>> ctx = app.test_request_context()4748Normally you would use the `with` statement to make this request object48Normally you would use the ``with`` statement to make this request object49active, but in the shell it's easier to use the50:meth:`~flask.ctx.RequestContext.push` and51:meth:`~flask.ctx.RequestContext.pop` methods by hand:52
51 unmodified lines
modified:docs/signals.rst+2 added-2 removed
75 unmodified lines
76Make sure to subscribe with an extra ``**extra`` argument so that your77calls don't fail if Flask introduces new arguments to the signals.7879All the template rendering in the code issued by the application `app`80in the body of the `with` block will now be recorded in the `templates`80in the body of the ``with`` block will now be recorded in the `templates`81variable. Whenever a template is rendered, the template object as well as82context are appended to it.8384Additionally there is a convenient helper method
62 unmodified lines
147 def save(self):148 model_saved.send(self)149150Try to always pick a good sender. If you have a class that is emitting a151signal, pass `self` as sender. If you are emitting a signal from a random151signal, pass ``self`` as sender. If you are emitting a signal from a random152function, you can pass ``current_app._get_current_object()`` as sender.153154.. admonition:: Passing Proxies as Senders155
197 unmodified lines
modified:docs/testing.rst+4 added-4 removed
200 unmodified lines
201--------------------202203Besides using the test client as shown above, there is also the204:meth:`~flask.Flask.test_request_context` method that can be used205in combination with the `with` statement to activate a request context205in combination with the ``with`` statement to activate a request context206temporarily. With this you can access the :class:`~flask.request`,207:class:`~flask.g` and :class:`~flask.session` objects like in view208functions. Here is a full example that demonstrates this approach::209
13 unmodified lines
223Note however that if you are using a test request context, the224:meth:`~flask.Flask.before_request` functions are not automatically called225same for :meth:`~flask.Flask.after_request` functions. However226:meth:`~flask.Flask.teardown_request` functions are indeed executed when227the test request context leaves the `with` block. If you do want the227the test request context leaves the ``with`` block. If you do want the228:meth:`~flask.Flask.before_request` functions to be called as well, you229need to call :meth:`~flask.Flask.preprocess_request` yourself::230231 app = flask.Flask(__name__)
75 unmodified lines
307308Sometimes it is helpful to trigger a regular request but still keep the309context around for a little longer so that additional introspection can310happen. With Flask 0.4 this is possible by using the311:meth:`~flask.Flask.test_client` with a `with` block::311:meth:`~flask.Flask.test_client` with a ``with`` block::312313 app = flask.Flask(__name__)314315 with app.test_client() as c:316 rv = c.get('/?tequila=42')317 assert request.args['tequila'] == '42'318319If you were to use just the :meth:`~flask.Flask.test_client` without320the `with` block, the `assert` would fail with an error because `request`320the ``with`` block, the ``assert`` would fail with an error because `request`321is no longer available (because you are trying to use it outside of the actual request).322323324Accessing and Modifying Sessions
29 unmodified lines
modified:docs/views.rst+1 added-1 removed
69 unmodified lines
70 return User.query.all()7172This of course is not that helpful for such a small example, but it's good73enough to explain the basic principle. When you have a class-based view74the question comes up what `self` points to. The way this works is that74the question comes up what ``self`` points to. The way this works is that75whenever the request is dispatched a new instance of the class is created76and the :meth:`~flask.views.View.dispatch_request` method is called with77the parameters from the URL rule. The class itself is instantiated with78the parameters passed to the :meth:`~flask.views.View.as_view` function.
159 unmodified lines
modified:flask/app.py+7 added-7 removed
836 unmodified lines
837838 app.testing = True839 client = app.test_client()840841 The test client can be used in a `with` block to defer the closing down842 of the context until the end of the `with` block. This is useful if841 The test client can be used in a ``with`` block to defer the closing down842 of the context until the end of the ``with`` block. This is useful if843 you want to access the context locals for testing::844845 with app.test_client() as c:846 rv = c.get('/?vodka=42')
15 unmodified lines
862863 See :class:`~flask.testing.FlaskClient` for more information.864865 .. versionchanged:: 0.4866 added support for `with` block usage for the client.866 added support for ``with`` block usage for the client.867868 .. versionadded:: 0.7869 The `use_cookies` parameter was added as well as the ability870 to override the client to be used by setting the
940 unmodified lines
18111812 def request_context(self, environ):1813 """Creates a :class:`~flask.ctx.RequestContext` from the given1814 environment and binds it to the current context. This must be used in1815 combination with the `with` statement because the request is only bound1816 to the current context for the duration of the `with` block.1815 combination with the ``with`` statement because the request is only bound1816 to the current context for the duration of the ``with`` block.18171818 Example usage::18191820 with app.request_context(environ):1821 do_something_with(request)18221823 The object returned can also be used without the `with` statement1823 The object returned can also be used without the ``with`` statement1824 which is useful for working in the shell. The example above is1825 doing exactly the same as this code::18261827 ctx = app.request_context(environ)
3 unmodified lines
1831 finally:1832 ctx.pop()18331834 .. versionchanged:: 0.31835 Added support for non-with statement usage and `with` statement1835 Added support for non-with statement usage and ``with`` statement1836 is now passed the ctx object.18371838 :param environ: a WSGI environment1839 """
70 unmodified lines