| 
									
										
										
										
											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: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-18 02:27:25 +08:00
										 |  |  | Blueprint Objects
 | 
					
						
							|  |  |  | -----------------
 | 
					
						
							| 
									
										
										
										
											2010-04-24 23:35:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-18 02:27:25 +08:00
										 |  |  | .. autoclass:: Blueprint
 | 
					
						
							| 
									
										
										
										
											2010-04-24 23:35:25 +08:00
										 |  |  |    :members: | 
					
						
							|  |  |  |    :inherited-members: | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Incoming Request Data
 | 
					
						
							|  |  |  | ---------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-12 01:18:40 +08:00
										 |  |  | .. autoclass:: Request
 | 
					
						
							| 
									
										
										
										
											2011-07-30 08:07:31 +08:00
										 |  |  |    :members: | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: form
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:13:01 +08:00
										 |  |  |       A :class:`~werkzeug.datastructures.MultiDict` with the parsed form data from ``POST``
 | 
					
						
							|  |  |  |       or ``PUT`` requests.  Please keep in mind that file uploads will not
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-09 21:16:41 +08:00
										 |  |  |    .. attribute:: headers
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       The incoming request headers as a dictionary like object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:13:01 +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
 | 
					
						
							| 
									
										
										
										
											2014-09-01 00:07:26 +08:00
										 |  |  |    .. attribute:: full_path
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |    .. attribute:: script_root
 | 
					
						
							|  |  |  |    .. attribute:: url
 | 
					
						
							|  |  |  |    .. attribute:: base_url
 | 
					
						
							|  |  |  |    .. attribute:: url_root
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 05:04:53 +08:00
										 |  |  |       Provides different ways to look at the current `IRI
 | 
					
						
							|  |  |  |       <http://tools.ietf.org/html/rfc3987>`_.  Imagine your application is
 | 
					
						
							|  |  |  |       listening on the following application root::
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           http://www.example.com/myapplication
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-21 19:52:42 +08:00
										 |  |  |       And a user requests the following URI::
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-21 19:52:42 +08:00
										 |  |  |           http://www.example.com/myapplication/%CF%80/page.html?x=y
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       In this case the values of the above mentioned attributes would be
 | 
					
						
							|  |  |  |       the following:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ============= ======================================================
 | 
					
						
							| 
									
										
										
										
											2015-01-21 14:00:38 +08:00
										 |  |  |       `path`        ``u'/π/page.html'``
 | 
					
						
							|  |  |  |       `full_path`   ``u'/π/page.html?x=y'``
 | 
					
						
							|  |  |  |       `script_root` ``u'/myapplication'``
 | 
					
						
							|  |  |  |       `base_url`    ``u'http://www.example.com/myapplication/π/page.html'``
 | 
					
						
							|  |  |  |       `url`         ``u'http://www.example.com/myapplication/π/page.html?x=y'``
 | 
					
						
							|  |  |  |       `url_root`    ``u'http://www.example.com/myapplication/'``
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       ============= ======================================================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  |    .. attribute:: is_xhr
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |       ``True`` if the request was triggered via a JavaScript
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  |       `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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-30 08:07:31 +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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This is a proxy.  See :ref:`notes-on-proxies` for more information.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The request object is an instance of a :class:`~werkzeug.wrappers.Request`
 | 
					
						
							|  |  |  |    subclass and provides all of the attributes Werkzeug defines.  This
 | 
					
						
							|  |  |  |    just shows a quick overview of the most important ones.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-12 01:18:40 +08:00
										 |  |  | Response Objects
 | 
					
						
							|  |  |  | ----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: flask.Response
 | 
					
						
							|  |  |  |    :members: set_cookie, data, mimetype
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: headers
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-28 22:51:53 +08:00
										 |  |  |       A :class:`~werkzeug.datastructures.Headers` object representing the response headers.
 | 
					
						
							| 
									
										
										
										
											2010-04-12 01:18:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-17 21:12:12 +08:00
										 |  |  |    .. attribute:: status
 | 
					
						
							| 
									
										
										
										
											2010-04-12 01:18:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-13 07:36:15 +08:00
										 |  |  |       A string with a response status.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: status_code
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-12 01:18:40 +08:00
										 |  |  |       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
 | 
					
						
							| 
									
										
										
										
											2011-06-28 20:34:07 +08:00
										 |  |  | not modify it unless they know the secret key, so make sure to set that
 | 
					
						
							| 
									
										
										
										
											2011-06-27 09:43:48 +08:00
										 |  |  | to something complex and unguessable.
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |       ``True`` if the session is new, ``False`` otherwise.
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: modified
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |       ``True`` if the session object detected a modification.  Be advised
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  |       that modifications on mutable structures are not picked up
 | 
					
						
							|  |  |  |       automatically, in that situation you have to explicitly set the
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |       attribute to ``True`` yourself.  Here an example::
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |           # 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +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
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |       default is 31 days.  If set to ``False`` (which is the default) the
 | 
					
						
							| 
									
										
										
										
											2010-10-31 08:55:41 +08:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-07 17:27:22 +08:00
										 |  |  | Session Interface
 | 
					
						
							|  |  |  | -----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-08 20:38:49 +08:00
										 |  |  | .. versionadded:: 0.8
 | 
					
						
							| 
									
										
										
										
											2011-07-07 17:27:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | The session interface provides a simple way to replace the session
 | 
					
						
							|  |  |  | implementation that Flask is using.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. currentmodule:: flask.sessions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: SessionInterface
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: SecureCookieSessionInterface
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-11 09:36:14 +08:00
										 |  |  | .. autoclass:: SecureCookieSession
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-07 17:27:22 +08:00
										 |  |  | .. autoclass:: NullSession
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: SessionMixin
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-14 18:33:36 +08:00
										 |  |  | .. autodata:: session_json_serializer
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This object provides dumping and loading methods similar to simplejson
 | 
					
						
							|  |  |  |    but it also tags certain builtin Python objects that commonly appear in
 | 
					
						
							|  |  |  |    sessions.  Currently the following extended values are supported in
 | 
					
						
							|  |  |  |    the JSON it dumps:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    -    :class:`~markupsafe.Markup` objects
 | 
					
						
							|  |  |  |    -    :class:`~uuid.UUID` objects
 | 
					
						
							|  |  |  |    -    :class:`~datetime.datetime` objects
 | 
					
						
							|  |  |  |    -   :class:`tuple`\s
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-26 01:12:41 +08:00
										 |  |  | .. admonition:: Notice
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer
 | 
					
						
							|  |  |  |    starting with Flask 0.8.  Either catch this down yourself or use
 | 
					
						
							|  |  |  |    the :attr:`~flask.Flask.permanent_session_lifetime` attribute on the
 | 
					
						
							|  |  |  |    app which converts the result to an integer automatically.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-07 17:27:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 22:18:39 +08:00
										 |  |  | Test Client
 | 
					
						
							|  |  |  | -----------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. currentmodule:: flask.testing
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 03:47:50 +08:00
										 |  |  | .. autoclass:: FlaskClient
 | 
					
						
							| 
									
										
										
										
											2011-08-25 22:18:39 +08:00
										 |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 07:32:39 +08:00
										 |  |  | Application Globals
 | 
					
						
							|  |  |  | -------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-07 17:27:22 +08:00
										 |  |  | .. currentmodule:: flask
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 07:32:39 +08:00
										 |  |  | 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 18:45:42 +08:00
										 |  |  |    Starting with Flask 0.10 this is stored on the application context and
 | 
					
						
							|  |  |  |    no longer on the request context which means it becomes available if
 | 
					
						
							| 
									
										
										
										
											2013-06-05 17:02:33 +08:00
										 |  |  |    only the application context is bound and not yet a request.  This
 | 
					
						
							|  |  |  |    is especially useful when combined with the :ref:`faking-resources`
 | 
					
						
							|  |  |  |    pattern for testing.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-09 19:06:33 +08:00
										 |  |  |    Additionally as of 0.10 you can use the :meth:`get` method to
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |    get an attribute or ``None`` (or the second argument) if it's not set.
 | 
					
						
							| 
									
										
										
										
											2013-06-09 19:06:33 +08:00
										 |  |  |    These two usages are now equivalent::
 | 
					
						
							| 
									
										
										
										
											2013-06-05 17:02:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         user = getattr(flask.g, 'user', None)
 | 
					
						
							| 
									
										
										
										
											2013-06-09 19:06:33 +08:00
										 |  |  |         user = flask.g.get('user', None)
 | 
					
						
							| 
									
										
										
										
											2012-12-21 18:45:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-09 19:23:14 +08:00
										 |  |  |    It's now also possible to use the ``in`` operator on it to see if an
 | 
					
						
							|  |  |  |    attribute is defined and it yields all keys on iteration.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2012-04-09 22:22:36 +08:00
										 |  |  |    by side.  This is powered by the application context and not by the
 | 
					
						
							|  |  |  |    request context, so you can change the value of this proxy by
 | 
					
						
							|  |  |  |    using the :meth:`~flask.Flask.app_context` method.
 | 
					
						
							| 
									
										
										
										
											2010-05-19 00:05:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 17:12:58 +08:00
										 |  |  |    This is a proxy.  See :ref:`notes-on-proxies` for more information.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-15 04:13:58 +08:00
										 |  |  | .. autofunction:: has_request_context
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-14 18:00:04 +08:00
										 |  |  | .. autofunction:: copy_current_request_context
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-09 22:22:36 +08:00
										 |  |  | .. autofunction:: has_app_context
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-08 20:14:32 +08:00
										 |  |  | .. autofunction:: after_this_request
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-10 17:27:42 +08:00
										 |  |  | .. autofunction:: send_file
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-05 16:25:51 +08:00
										 |  |  | .. autofunction:: send_from_directory
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-04 22:53:04 +08:00
										 |  |  | .. autofunction:: safe_join
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | .. autofunction:: escape
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: Markup
 | 
					
						
							|  |  |  |    :members: escape, unescape, striptags
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Message Flashing
 | 
					
						
							|  |  |  | ----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: flash
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: get_flashed_messages
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | JSON Support
 | 
					
						
							|  |  |  | ------------
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. module:: flask.json
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Flask uses ``simplejson`` for the JSON implementation.  Since simplejson
 | 
					
						
							| 
									
										
										
										
											2014-12-10 01:16:10 +08:00
										 |  |  | is provided by both the standard library as well as extension, Flask will
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | try simplejson first and then fall back to the stdlib json module.  On top
 | 
					
						
							| 
									
										
										
										
											2013-08-11 21:59:46 +08:00
										 |  |  | of that it will delegate access to the current application's JSON encoders
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | and decoders for easier customization.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | So for starters instead of doing::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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 in the standard
 | 
					
						
							| 
									
										
										
										
											2013-07-22 17:12:17 +08:00
										 |  |  | library.  The following extensions are by default applied to the stdlib's
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | JSON module:
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | 1.  ``datetime`` objects are serialized as :rfc:`822` strings.
 | 
					
						
							|  |  |  | 2.  Any object with an ``__html__`` method (like :class:`~flask.Markup`)
 | 
					
						
							| 
									
										
										
										
											2013-06-23 08:36:20 +08:00
										 |  |  |     will have that method called and then the return value is serialized
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  |     as string.
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | The :func:`~htmlsafe_dumps` function of this json module is also available
 | 
					
						
							| 
									
										
										
										
											2014-11-05 12:56:03 +08:00
										 |  |  | as filter called ``|tojson`` in Jinja2.  Note that inside ``script``
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | tags no escaping must take place, so make sure to disable escaping
 | 
					
						
							| 
									
										
										
										
											2014-11-05 12:56:03 +08:00
										 |  |  | with ``|safe`` if you intend to use it inside ``script`` tags unless
 | 
					
						
							| 
									
										
										
										
											2013-06-03 19:25:08 +08:00
										 |  |  | you are using Flask 0.10 which implies that:
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. sourcecode:: html+jinja
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  |     <script type=text/javascript>
 | 
					
						
							|  |  |  |         doSomethingWith({{ user.username|tojson|safe }});
 | 
					
						
							|  |  |  |     </script>
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 09:21:24 +08:00
										 |  |  | .. admonition:: Auto-Sort JSON Keys
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 00:27:26 +08:00
										 |  |  |     The configuration variable ``JSON_SORT_KEYS`` (:ref:`config`) can be
 | 
					
						
							|  |  |  |     set to false to stop Flask from auto-sorting keys.  By default sorting
 | 
					
						
							|  |  |  |     is enabled and outside of the app context sorting is turned on.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Notice that disabling key sorting can cause issues when using content
 | 
					
						
							|  |  |  |     based HTTP caches and Python's hash randomization feature.
 | 
					
						
							| 
									
										
										
										
											2014-01-25 09:21:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. autofunction:: jsonify
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: dumps
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. autofunction:: dump
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. autofunction:: loads
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. autofunction:: load
 | 
					
						
							| 
									
										
										
										
											2010-04-19 21:39:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. autoclass:: JSONEncoder
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: JSONDecoder
 | 
					
						
							|  |  |  |    :members: | 
					
						
							| 
									
										
										
										
											2010-04-20 00:51:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | Template Rendering
 | 
					
						
							|  |  |  | ------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:31:48 +08:00
										 |  |  | .. currentmodule:: flask
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-09 01:03:15 +08:00
										 |  |  | .. 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-18 18:51:10 +08:00
										 |  |  | Extensions
 | 
					
						
							|  |  |  | ----------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: flask.ext
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This module acts as redirect import module to Flask extensions.  It was
 | 
					
						
							|  |  |  |    added in 0.8 as the canonical way to import Flask extensions and makes
 | 
					
						
							|  |  |  |    it possible for us to have more flexibility in how we distribute
 | 
					
						
							|  |  |  |    extensions.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    If you want to use an extension named “Flask-Foo” you would import it
 | 
					
						
							|  |  |  |    from :data:`~flask.ext` as follows::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         from flask.ext import foo
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 0.8
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  | Stream Helpers
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: stream_with_context
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-31 23:50:31 +08:00
										 |  |  | Useful Internals
 | 
					
						
							|  |  |  | ----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-28 02:10:53 +08:00
										 |  |  | .. autoclass:: flask.ctx.RequestContext
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-31 23:50:31 +08:00
										 |  |  | .. 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-09 22:22:36 +08:00
										 |  |  | .. autoclass:: flask.ctx.AppContext
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: _app_ctx_stack
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Works similar to the request context but only binds the application.
 | 
					
						
							|  |  |  |    This is mainly there for extensions to store data.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 0.9
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-18 02:27:25 +08:00
										 |  |  | .. autoclass:: flask.blueprints.BlueprintSetupState
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |    ``True`` if the signaling system is available.  This is the case
 | 
					
						
							| 
									
										
										
										
											2010-07-17 20:39:28 +08:00
										 |  |  |    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`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-28 02:10:53 +08:00
										 |  |  | .. data:: request_tearing_down
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent when the application is tearing down the request.
 | 
					
						
							| 
									
										
										
										
											2012-04-10 00:29:00 +08:00
										 |  |  |    This is always called, even if an error happened.  An `exc` keyword
 | 
					
						
							|  |  |  |    argument is passed with the exception that caused the teardown.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionchanged:: 0.9
 | 
					
						
							|  |  |  |       The `exc` parameter was added.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: appcontext_tearing_down
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent when the application is tearing down the
 | 
					
						
							|  |  |  |    application context.  This is always called, even if an error happened.
 | 
					
						
							|  |  |  |    An `exc` keyword argument is passed with the exception that caused the
 | 
					
						
							| 
									
										
										
										
											2013-06-05 16:53:26 +08:00
										 |  |  |    teardown.  The sender is the application.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: appcontext_pushed
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent when an application context is pushed.  The sender
 | 
					
						
							|  |  |  |    is the application.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 0.10
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: appcontext_popped
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent when an application context is popped.  The sender
 | 
					
						
							|  |  |  |    is the application.  This usually falls in line with the
 | 
					
						
							|  |  |  |    :data:`appcontext_tearing_down` signal.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 0.10
 | 
					
						
							| 
									
										
										
										
											2011-05-28 02:10:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-22 05:04:06 +08:00
										 |  |  | .. data:: message_flashed
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This signal is sent when the application is flashing a message.  The
 | 
					
						
							|  |  |  |    messages is sent as `message` keyword argument and the category as
 | 
					
						
							|  |  |  |    `category`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionadded:: 0.10
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-26 07:30:28 +08:00
										 |  |  | .. _blinker: https://pypi.python.org/pypi/blinker
 | 
					
						
							| 
									
										
										
										
											2011-06-28 20:24:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-31 20:54:46 +08:00
										 |  |  | Class-Based Views
 | 
					
						
							| 
									
										
										
										
											2011-06-28 20:24:54 +08:00
										 |  |  | -----------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. versionadded:: 0.7
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. currentmodule:: None
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: flask.views.View
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: flask.views.MethodView
 | 
					
						
							|  |  |  |    :members: | 
					
						
							| 
									
										
										
										
											2011-07-14 20:25:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  | .. _url-route-registrations:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | URL Route Registrations
 | 
					
						
							|  |  |  | -----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Generally there are three ways to define rules for the routing system:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 1.  You can use the :meth:`flask.Flask.route` decorator.
 | 
					
						
							|  |  |  | 2.  You can use the :meth:`flask.Flask.add_url_rule` function.
 | 
					
						
							|  |  |  | 3.  You can directly access the underlying Werkzeug routing system
 | 
					
						
							|  |  |  |     which is exposed as :attr:`flask.Flask.url_map`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:56:15 +08:00
										 |  |  | Variable parts in the route can be specified with angular brackets
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  | (``/user/<username>``).  By default a variable part in the URL accepts any
 | 
					
						
							|  |  |  | string without a slash however a different converter can be specified as
 | 
					
						
							|  |  |  | well by using ``<converter:name>``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Variable parts are passed to the view function as keyword arguments.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:56:15 +08:00
										 |  |  | The following converters are available:
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | =========== ===============================================
 | 
					
						
							| 
									
										
										
										
											2012-03-13 06:21:49 +08:00
										 |  |  | `string`    accepts any text without a slash (the default)
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  | `int`       accepts integers
 | 
					
						
							|  |  |  | `float`     like `int` but for floating point values
 | 
					
						
							|  |  |  | `path`      like the default but also accepts slashes
 | 
					
						
							|  |  |  | =========== ===============================================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-26 01:24:37 +08:00
										 |  |  | Here are some examples::
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/')
 | 
					
						
							|  |  |  |     def index():
 | 
					
						
							|  |  |  |         pass
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/<username>')
 | 
					
						
							|  |  |  |     def show_user(username):
 | 
					
						
							|  |  |  |         pass
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/post/<int:post_id>')
 | 
					
						
							|  |  |  |     def show_post(post_id):
 | 
					
						
							|  |  |  |         pass
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An important detail to keep in mind is how Flask deals with trailing
 | 
					
						
							|  |  |  | slashes.  The idea is to keep each URL unique so the following rules
 | 
					
						
							|  |  |  | apply:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 1. If a rule ends with a slash and is requested without a slash by the
 | 
					
						
							|  |  |  |    user, the user is automatically redirected to the same page with a
 | 
					
						
							|  |  |  |    trailing slash attached.
 | 
					
						
							| 
									
										
										
										
											2011-09-26 01:24:37 +08:00
										 |  |  | 2. If a rule does not end with a trailing slash and the user requests the
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  |    page with a trailing slash, a 404 not found is raised.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is consistent with how web servers deal with static files.  This
 | 
					
						
							|  |  |  | also makes it possible to use relative link targets safely.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | You can also define multiple rules for the same function.  They have to be
 | 
					
						
							|  |  |  | unique however.  Defaults can also be specified.  Here for example is a
 | 
					
						
							|  |  |  | definition for a URL that accepts an optional page::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.route('/users/', defaults={'page': 1})
 | 
					
						
							|  |  |  |     @app.route('/users/page/<int:page>')
 | 
					
						
							|  |  |  |     def show_users(page):
 | 
					
						
							|  |  |  |         pass
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This specifies that ``/users/`` will be the URL for page one and
 | 
					
						
							|  |  |  | ``/users/page/N`` will be the URL for page `N`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-26 01:01:30 +08:00
										 |  |  | Here are the parameters that :meth:`~flask.Flask.route` and
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  | :meth:`~flask.Flask.add_url_rule` accept.  The only difference is that
 | 
					
						
							|  |  |  | with the route parameter the view function is defined with the decorator
 | 
					
						
							|  |  |  | instead of the `view_func` parameter.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | =============== ==========================================================
 | 
					
						
							| 
									
										
										
										
											2012-04-18 10:28:28 +08:00
										 |  |  | `rule`          the URL rule as string
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  | `endpoint`      the endpoint for the registered URL rule.  Flask itself
 | 
					
						
							|  |  |  |                 assumes that the name of the view function is the name
 | 
					
						
							|  |  |  |                 of the endpoint if not explicitly stated.
 | 
					
						
							|  |  |  | `view_func`     the function to call when serving a request to the
 | 
					
						
							|  |  |  |                 provided endpoint.  If this is not provided one can
 | 
					
						
							|  |  |  |                 specify the function later by storing it in the
 | 
					
						
							|  |  |  |                 :attr:`~flask.Flask.view_functions` dictionary with the
 | 
					
						
							|  |  |  |                 endpoint as key.
 | 
					
						
							|  |  |  | `defaults`      A dictionary with defaults for this rule.  See the
 | 
					
						
							|  |  |  |                 example above for how defaults work.
 | 
					
						
							|  |  |  | `subdomain`     specifies the rule for the subdomain in case subdomain
 | 
					
						
							|  |  |  |                 matching is in use.  If not specified the default
 | 
					
						
							|  |  |  |                 subdomain is assumed.
 | 
					
						
							|  |  |  | `**options`     the options to be forwarded to the underlying
 | 
					
						
							|  |  |  |                 :class:`~werkzeug.routing.Rule` object.  A change to
 | 
					
						
							|  |  |  |                 Werkzeug is handling of method options.  methods is a list
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:13:01 +08:00
										 |  |  |                 of methods this rule should be limited to (``GET``, ``POST``
 | 
					
						
							|  |  |  |                 etc.).  By default a rule just listens for ``GET`` (and
 | 
					
						
							|  |  |  |                 implicitly ``HEAD``).  Starting with Flask 0.6, ``OPTIONS`` is
 | 
					
						
							| 
									
										
										
										
											2011-09-26 00:49:00 +08:00
										 |  |  |                 implicitly added and handled by the standard request
 | 
					
						
							|  |  |  |                 handling.  They have to be specified as keyword arguments.
 | 
					
						
							|  |  |  | =============== ==========================================================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:25:14 +08:00
										 |  |  | .. _view-func-options:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | View Function Options
 | 
					
						
							|  |  |  | ---------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For internal usage the view functions can have some attributes attached to
 | 
					
						
							|  |  |  | customize behavior the view function would normally not have control over.
 | 
					
						
							|  |  |  | The following attributes can be provided optionally to either override
 | 
					
						
							|  |  |  | some defaults to :meth:`~flask.Flask.add_url_rule` or general behavior:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   `__name__`: The name of a function is by default used as endpoint.  If
 | 
					
						
							|  |  |  |     endpoint is provided explicitly this value is used.  Additionally this
 | 
					
						
							|  |  |  |     will be prefixed with the name of the blueprint by default which
 | 
					
						
							|  |  |  |     cannot be customized from the function itself.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   `methods`: If methods are not provided when the URL rule is added,
 | 
					
						
							| 
									
										
										
										
											2014-12-05 02:53:30 +08:00
										 |  |  |     Flask will look on the view function object itself if a `methods`
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:25:14 +08:00
										 |  |  |     attribute exists.  If it does, it will pull the information for the
 | 
					
						
							|  |  |  |     methods from there.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   `provide_automatic_options`: if this attribute is set Flask will
 | 
					
						
							|  |  |  |     either force enable or disable the automatic implementation of the
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:13:01 +08:00
										 |  |  |     HTTP ``OPTIONS`` response.  This can be useful when working with
 | 
					
						
							|  |  |  |     decorators that want to customize the ``OPTIONS`` response on a per-view
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:25:14 +08:00
										 |  |  |     basis.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-08 19:51:26 +08:00
										 |  |  | -   `required_methods`: if this attribute is set, Flask will always add
 | 
					
						
							|  |  |  |     these methods when registering a URL rule even if the methods were
 | 
					
						
							| 
									
										
										
										
											2013-07-22 17:12:17 +08:00
										 |  |  |     explicitly overridden in the ``route()`` call.
 | 
					
						
							| 
									
										
										
										
											2012-05-08 19:51:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:25:14 +08:00
										 |  |  | Full example::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def index():
 | 
					
						
							|  |  |  |         if request.method == 'OPTIONS':
 | 
					
						
							|  |  |  |             # custom options handling here
 | 
					
						
							|  |  |  |             ...
 | 
					
						
							|  |  |  |         return 'Hello World!'
 | 
					
						
							|  |  |  |     index.provide_automatic_options = False
 | 
					
						
							|  |  |  |     index.methods = ['GET', 'OPTIONS']
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app.add_url_rule('/', index)
 | 
					
						
							| 
									
										
										
										
											2011-07-14 20:25:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. versionadded:: 0.8
 | 
					
						
							|  |  |  |    The `provide_automatic_options` functionality was added.
 | 
					
						
							| 
									
										
										
										
											2014-05-08 03:35:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Command Line Interface
 | 
					
						
							|  |  |  | ----------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. currentmodule:: flask.cli
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autoclass:: FlaskGroup
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-25 22:50:22 +08:00
										 |  |  | .. autoclass:: AppGroup
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-08 03:35:51 +08:00
										 |  |  | .. autoclass:: ScriptInfo
 | 
					
						
							|  |  |  |    :members: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-25 22:50:22 +08:00
										 |  |  | .. autofunction:: with_appcontext
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-08 03:35:51 +08:00
										 |  |  | .. autofunction:: pass_script_info
 | 
					
						
							| 
									
										
										
										
											2014-08-28 22:51:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-26 16:03:15 +08:00
										 |  |  |    Marks a function so that an instance of :class:`ScriptInfo` is passed
 | 
					
						
							|  |  |  |    as first argument to the click callback.
 | 
					
						
							| 
									
										
										
										
											2014-05-08 03:35:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. autofunction:: script_info_option
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A special decorator that informs a click callback to be passed the
 | 
					
						
							|  |  |  |    script info object as first argument.  This is normally not useful
 | 
					
						
							|  |  |  |    unless you implement very special commands like the run command which
 | 
					
						
							| 
									
										
										
										
											2014-11-05 11:04:58 +08:00
										 |  |  |    does not want the application to be loaded yet.
 | 
					
						
							| 
									
										
										
										
											2014-05-08 03:35:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. autodata:: run_command
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. autodata:: shell_command
 |