| 
									
										
										
										
											2010-04-09 19:40:05 +08:00
										 |  |  | .. _api:
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-06 22:02:14 +08:00
										 |  |  | API
 | 
					
						
							|  |  |  | ===
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. module:: flask
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This part of the documentation covers all the interfaces of Flask.  For
 | 
					
						
							|  |  |  | parts where Flask depends on external libraries, we document the most
 | 
					
						
							|  |  |  | important right here and provide links to the canonical documentation.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | Application Object
 | 
					
						
							|  |  |  | ------------------
 | 
					
						
							| 
									
										
										
										
											2010-04-06 22:02:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: Flask
 | 
					
						
							|  |  |  |    :members: | 
					
						
							| 
									
										
										
										
											2010-04-24 23:35:25 +08:00
										 |  |  |    :inherited-members: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Module Objects
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: Module
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  |    :inherited-members: | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Incoming Request Data
 | 
					
						
							|  |  |  | ---------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-12 01:18:40 +08:00
										 |  |  | .. autoclass:: Request
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | .. class:: request
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    To access incoming request data, you can use the global `request`
 | 
					
						
							|  |  |  |    object.  Flask parses incoming request data for you and gives you
 | 
					
						
							|  |  |  |    access to it through that global object.  Internally Flask makes
 | 
					
						
							|  |  |  |    sure that you always get the correct data for the active thread if you
 | 
					
						
							|  |  |  |    are in a multithreaded environment.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  |    This is a proxy.  See :ref:`notes-on-proxies` for more information.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |    The request object is an instance of a :class:`~werkzeug.wrappers.Request`
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |    subclass and provides all of the attributes Werkzeug defines.  This
 | 
					
						
							|  |  |  |    just shows a quick overview of the most important ones.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: form
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |       A :class:`~werkzeug.datastructures.MultiDict` with the parsed form data from `POST`
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       or `PUT` requests.  Please keep in mind that file uploads will not
 | 
					
						
							|  |  |  |       end up here,  but instead in the :attr:`files` attribute.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: args
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |       A :class:`~werkzeug.datastructures.MultiDict` with the parsed contents of the query
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       string.  (The part in the URL after the question mark).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: values
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |       A :class:`~werkzeug.datastructures.CombinedMultiDict` with the contents of both
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       :attr:`form` and :attr:`args`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: cookies
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       A :class:`dict` with the contents of all cookies transmitted with
 | 
					
						
							|  |  |  |       the request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: stream
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  |       If the incoming form data was not encoded with a known mimetype
 | 
					
						
							|  |  |  |       the data is stored unmodified in this stream for consumption.  Most
 | 
					
						
							|  |  |  |       of the time it is a better idea to use :attr:`data` which will give
 | 
					
						
							|  |  |  |       you that data as a string.  The stream only returns the data once.
 | 
					
						
							| 
									
										
										
										
											2010-05-30 02:06:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  |    .. attribute:: data
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  |       Contains the incoming request data as string in case it came with
 | 
					
						
							|  |  |  |       a mimetype Flask does not handle.
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: files
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |       A :class:`~werkzeug.datastructures.MultiDict` with files uploaded as part of a
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       `POST` or `PUT` request.  Each file is stored as
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |       :class:`~werkzeug.datastructures.FileStorage` object.  It basically behaves like a
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       standard file object you know from Python, with the difference that
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |       it also has a :meth:`~werkzeug.datastructures.FileStorage.save` function that can
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       store the file on the filesystem.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 07:32:39 +08:00
										 |  |  |    .. attribute:: environ
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       The underlying WSGI environment.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |    .. attribute:: method
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       The current request method (``POST``, ``GET`` etc.)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: path
 | 
					
						
							|  |  |  |    .. attribute:: script_root
 | 
					
						
							|  |  |  |    .. attribute:: url
 | 
					
						
							|  |  |  |    .. attribute:: base_url
 | 
					
						
							|  |  |  |    .. attribute:: url_root
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Provides different ways to look at the current URL.  Imagine your
 | 
					
						
							|  |  |  |       application is listening on the following URL::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           http://www.example.com/myapplication
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       And a user requests the following URL::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           http://www.example.com/myapplication/page.html?x=y
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       In this case the values of the above mentioned attributes would be
 | 
					
						
							|  |  |  |       the following:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ============= ======================================================
 | 
					
						
							|  |  |  |       `path`        ``/page.html``
 | 
					
						
							|  |  |  |       `script_root` ``/myapplication``
 | 
					
						
							| 
									
										
										
										
											2010-05-04 17:43:19 +08:00
										 |  |  |       `base_url`    ``http://www.example.com/myapplication/page.html``
 | 
					
						
							|  |  |  |       `url`         ``http://www.example.com/myapplication/page.html?x=y``
 | 
					
						
							| 
									
										
										
										
											2010-04-20 05:49:02 +08:00
										 |  |  |       `url_root`    ``http://www.example.com/myapplication/``
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       ============= ======================================================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  |    .. attribute:: is_xhr
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       `True` if the request was triggered via a JavaScript
 | 
					
						
							|  |  |  |       `XMLHttpRequest`. This only works with libraries that support the
 | 
					
						
							|  |  |  |       ``X-Requested-With`` header and set it to `XMLHttpRequest`.
 | 
					
						
							|  |  |  |       Libraries that do that are prototype, jQuery and Mochikit and
 | 
					
						
							|  |  |  |       probably some more.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: json
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Contains the parsed body of the JSON request if the mimetype of
 | 
					
						
							|  |  |  |       the incoming data was `application/json`.  This requires Python 2.6
 | 
					
						
							|  |  |  |       or an installed version of simplejson.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-12 01:18:40 +08:00
										 |  |  | Response Objects
 | 
					
						
							|  |  |  | ----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: flask.Response
 | 
					
						
							|  |  |  |    :members: set_cookie, data, mimetype
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: headers
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       A :class:`Headers` object representing the response headers.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: status_code
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       The response status as integer.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Sessions
 | 
					
						
							|  |  |  | --------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you have the :attr:`Flask.secret_key` set you can use sessions in Flask
 | 
					
						
							|  |  |  | applications.  A session basically makes it possible to remember
 | 
					
						
							|  |  |  | information from one request to another.  The way Flask does this is by
 | 
					
						
							|  |  |  | using a signed cookie.  So the user can look at the session contents, but
 | 
					
						
							|  |  |  | not modify it unless he knows the secret key, so make sure to set that to
 | 
					
						
							|  |  |  | something complex and unguessable.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To access the current session you can use the :class:`session` object:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: session
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The session object works pretty much like an ordinary dict, with the
 | 
					
						
							|  |  |  |    difference that it keeps track on modifications.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  |    This is a proxy.  See :ref:`notes-on-proxies` for more information.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |    The following attributes are interesting:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: new
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       `True` if the session is new, `False` otherwise.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: modified
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       `True` if the session object detected a modification.  Be advised
 | 
					
						
							|  |  |  |       that modifications on mutable structures are not picked up
 | 
					
						
							|  |  |  |       automatically, in that situation you have to explicitly set the
 | 
					
						
							|  |  |  |       attribute to `True` yourself.  Here an example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # this change is not picked up because a mutable object (here
 | 
					
						
							|  |  |  |           # a list) is changed.
 | 
					
						
							|  |  |  |           session['objects'].append(42)
 | 
					
						
							|  |  |  |           # so mark it as modified yourself
 | 
					
						
							|  |  |  |           session.modified = True
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-31 08:55:41 +08:00
										 |  |  |    .. attribute:: permanent
 | 
					
						
							| 
									
										
										
										
											2010-04-27 20:32:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-10 09:13:39 +08:00
										 |  |  |       If set to `True` the session lives for
 | 
					
						
							| 
									
										
										
										
											2010-10-31 08:55:41 +08:00
										 |  |  |       :attr:`~flask.Flask.permanent_session_lifetime` seconds.  The
 | 
					
						
							|  |  |  |       default is 31 days.  If set to `False` (which is the default) the
 | 
					
						
							|  |  |  |       session will be deleted when the user closes the browser.
 | 
					
						
							| 
									
										
										
										
											2010-04-27 20:32:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 07:32:39 +08:00
										 |  |  | Application Globals
 | 
					
						
							|  |  |  | -------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | To share data that is valid for one request only from one function to
 | 
					
						
							|  |  |  | another, a global variable is not good enough because it would break in
 | 
					
						
							|  |  |  | threaded environments.  Flask provides you with a special object that
 | 
					
						
							|  |  |  | ensures it is only valid for the active request and that will return
 | 
					
						
							|  |  |  | different values for each request.  In a nutshell: it does the right
 | 
					
						
							|  |  |  | thing, like it does for :class:`request` and :class:`session`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: g
 | 
					
						
							| 
									
										
										
										
											2010-04-21 00:40:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 07:32:39 +08:00
										 |  |  |    Just store on this whatever you want.  For example a database
 | 
					
						
							|  |  |  |    connection or the user that is currently logged in.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  |    This is a proxy.  See :ref:`notes-on-proxies` for more information.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 07:32:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | Useful Functions and Classes
 | 
					
						
							|  |  |  | ----------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-19 00:05:05 +08:00
										 |  |  | .. data:: current_app
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Points to the application handling the request.  This is useful for
 | 
					
						
							|  |  |  |    extensions that want to support multiple applications running side
 | 
					
						
							|  |  |  |    by side.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  |    This is a proxy.  See :ref:`notes-on-proxies` for more information.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | .. autofunction:: url_for
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-11 08:20:10 +08:00
										 |  |  | .. function:: abort(code)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |    Raises an :exc:`~werkzeug.exceptions.HTTPException` for the given
 | 
					
						
							| 
									
										
										
										
											2010-04-11 08:20:10 +08:00
										 |  |  |    status code.  For example to abort request handling with a page not
 | 
					
						
							|  |  |  |    found exception, you would call ``abort(404)``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :param code: the HTTP error code.
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: redirect
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-17 17:36:16 +08:00
										 |  |  | .. autofunction:: make_response
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-10 17:27:42 +08:00
										 |  |  | .. autofunction:: send_file
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-05 16:25:51 +08:00
										 |  |  | .. autofunction:: send_from_directory
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | .. autofunction:: escape
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: Markup
 | 
					
						
							|  |  |  |    :members: escape, unescape, striptags
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Message Flashing
 | 
					
						
							|  |  |  | ----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: flash
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: get_flashed_messages
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | Returning JSON
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: jsonify
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: json
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If JSON support is picked up, this will be the module that Flask is
 | 
					
						
							|  |  |  |     using to parse and serialize JSON.  So instead of doing this yourself::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try:
 | 
					
						
							|  |  |  |             import simplejson as json
 | 
					
						
							|  |  |  |         except ImportError:
 | 
					
						
							|  |  |  |             import json
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     You can instead just do this::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         from flask import json
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For usage examples, read the :mod:`json` documentation.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The :func:`~json.dumps` function of this json module is also available
 | 
					
						
							|  |  |  |     as filter called ``|tojson`` in Jinja2.  Note that inside `script`
 | 
					
						
							|  |  |  |     tags no escaping must take place, so make sure to disable escaping
 | 
					
						
							|  |  |  |     with ``|safe`` if you intend to use it inside `script` tags:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .. sourcecode:: html+jinja
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         <script type=text/javascript>
 | 
					
						
							|  |  |  |             doSomethingWith({{ user.username|tojson|safe }});
 | 
					
						
							|  |  |  |         </script>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-20 00:51:04 +08:00
										 |  |  |     Note that the ``|tojson`` filter escapes forward slashes properly.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | Template Rendering
 | 
					
						
							|  |  |  | ------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: render_template
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: render_template_string
 | 
					
						
							| 
									
										
										
										
											2010-04-18 21:44:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: get_template_attribute
 | 
					
						
							| 
									
										
										
										
											2010-05-18 08:36:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Configuration
 | 
					
						
							|  |  |  | -------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: Config
 | 
					
						
							| 
									
										
										
										
											2010-05-27 23:42:01 +08:00
										 |  |  |    :members: | 
					
						
							| 
									
										
										
										
											2010-05-31 23:50:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Useful Internals
 | 
					
						
							|  |  |  | ----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: _request_ctx_stack
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  |    The internal :class:`~werkzeug.local.LocalStack` that is used to implement
 | 
					
						
							| 
									
										
										
										
											2010-05-31 23:50:31 +08:00
										 |  |  |    all the context local objects used in Flask.  This is a documented
 | 
					
						
							|  |  |  |    instance and can be used by extensions and application code but the
 | 
					
						
							|  |  |  |    use is discouraged in general.
 | 
					
						
							| 
									
										
										
										
											2010-06-03 21:26:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-29 21:25:20 +08:00
										 |  |  |    The following attributes are always present on each layer of the
 | 
					
						
							| 
									
										
										
										
											2010-06-29 21:59:55 +08:00
										 |  |  |    stack:
 | 
					
						
							| 
									
										
										
										
											2010-06-29 21:25:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |    `app`
 | 
					
						
							|  |  |  |       the active Flask application.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    `url_adapter`
 | 
					
						
							|  |  |  |       the URL adapter that was used to match the request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    `request`
 | 
					
						
							|  |  |  |       the current request object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    `session`
 | 
					
						
							|  |  |  |       the active session object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    `g`
 | 
					
						
							|  |  |  |       an object with all the attributes of the :data:`flask.g` object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    `flashes`
 | 
					
						
							|  |  |  |       an internal cache for the flashed messages.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Example usage::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       from flask import _request_ctx_stack
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def get_session():
 | 
					
						
							|  |  |  |           ctx = _request_ctx_stack.top
 | 
					
						
							|  |  |  |           if ctx is not None:
 | 
					
						
							|  |  |  |               return ctx.session
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 21:26:07 +08:00
										 |  |  |    .. versionchanged:: 0.4
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The request context is automatically popped at the end of the request
 | 
					
						
							|  |  |  |    for you.  In debug mode the request context is kept around if
 | 
					
						
							|  |  |  |    exceptions happen so that interactive debuggers have a chance to
 | 
					
						
							|  |  |  |    introspect the data.  With 0.4 this can also be forced for requests
 | 
					
						
							|  |  |  |    that did not fail and outside of `DEBUG` mode.  By setting
 | 
					
						
							|  |  |  |    ``'flask._preserve_context'`` to `True` on the WSGI environment the
 | 
					
						
							|  |  |  |    context will not pop itself at the end of the request.  This is used by
 | 
					
						
							|  |  |  |    the :meth:`~flask.Flask.test_client` for example to implement the
 | 
					
						
							|  |  |  |    deferred cleanup functionality.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    You might find this helpful for unittests where you need the
 | 
					
						
							|  |  |  |    information from the context local around for a little longer.  Make
 | 
					
						
							|  |  |  |    sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in
 | 
					
						
							|  |  |  |    that situation, otherwise your unittests will leak memory.
 | 
					
						
							| 
									
										
										
										
											2010-07-17 20:39:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Signals
 | 
					
						
							|  |  |  | -------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-17 21:25:53 +08:00
										 |  |  | .. when modifying this list, also update the one in signals.rst
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-17 20:39:28 +08:00
										 |  |  | .. versionadded:: 0.6
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: signals_available
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    `True` if the signalling system is available.  This is the case
 | 
					
						
							|  |  |  |    when `blinker`_ is installed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: template_rendered
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent when a template was successfully rendered.  The
 | 
					
						
							|  |  |  |    signal is invoked with the instance of the template as `template`
 | 
					
						
							|  |  |  |    and the context as dictionary (named `context`).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: request_started
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent before any request processing started but when the
 | 
					
						
							|  |  |  |    request context was set up.  Because the request context is already
 | 
					
						
							|  |  |  |    bound, the subscriber can access the request with the standard global
 | 
					
						
							|  |  |  |    proxies such as :class:`~flask.request`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: request_finished
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent right before the response is sent to the client.
 | 
					
						
							|  |  |  |    It is passed the response to be sent named `response`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: got_request_exception
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent when an exception happens during request processing.
 | 
					
						
							|  |  |  |    It is sent *before* the standard exception handling kicks in and even
 | 
					
						
							|  |  |  |    in debug mode, where no exception handling happens.  The exception
 | 
					
						
							|  |  |  |    itself is passed to the subscriber as `exception`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  | .. currentmodule:: None
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-17 20:39:28 +08:00
										 |  |  | .. class:: flask.signals.Namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    An alias for :class:`blinker.base.Namespace` if blinker is available,
 | 
					
						
							|  |  |  |    otherwise a dummy class that creates fake signals.  This class is
 | 
					
						
							|  |  |  |    available for Flask extensions that want to provide the same fallback
 | 
					
						
							|  |  |  |    system as Flask itself.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. method:: signal(name, doc=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Creates a new signal for this namespace if blinker is available,
 | 
					
						
							|  |  |  |       otherwise returns a fake signal that has a send method that will
 | 
					
						
							|  |  |  |       do nothing but will fail with a :exc:`RuntimeError` for all other
 | 
					
						
							|  |  |  |       operations, including connecting.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. _blinker: http://pypi.python.org/pypi/blinker
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. _notes-on-proxies:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Notes On Proxies
 | 
					
						
							|  |  |  | ----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Some of the objects provided by Flask are proxies to other objects.  The
 | 
					
						
							| 
									
										
										
										
											2010-10-06 09:47:48 +08:00
										 |  |  | reason behind this is that these proxies are shared between threads and
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  | they have to dispatch to the actual object bound to a thread behind the
 | 
					
						
							|  |  |  | scenes as necessary.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Most of the time you don't have to care about that, but there are some
 | 
					
						
							|  |  |  | exceptions where it is good to know that this object is an actual proxy:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   The proxy objects do not fake their inherited types, so if you want to
 | 
					
						
							|  |  |  |     perform actual instance checks, you have to do that on the instance
 | 
					
						
							| 
									
										
										
										
											2010-10-06 09:47:48 +08:00
										 |  |  |     that is being proxied (see `_get_current_object` below).
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  | -   if the object reference is important (so for example for sending
 | 
					
						
							|  |  |  |     :ref:`signals`)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If you need to get access to the underlying object that is proxied, you
 | 
					
						
							| 
									
										
										
										
											2011-03-14 08:32:19 +08:00
										 |  |  | can use the :meth:`~werkzeug.local.LocalProxy._get_current_object` method::
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     app = current_app._get_current_object()
 | 
					
						
							|  |  |  |     my_signal.send(app)
 |