Skip to content
Myr

api.rst

This file

Path

docs/api.rst

209 lines · 6.1 KiB

Last changed by

Added testing documentationArmin Ronacherbefore adoption03148db

History of this file

Take it

1.. _api:23API4===56.. module:: flask78This part of the documentation covers all the interfaces of Flask.  For9parts where Flask depends on external libraries, we document the most10important right here and provide links to the canonical documentation.111213Application Object14------------------1516.. autoclass:: Flask17   :members:1819Incoming Request Data20---------------------2122.. autoclass:: Request2324.. class:: request2526   To access incoming request data, you can use the global `request`27   object.  Flask parses incoming request data for you and gives you28   access to it through that global object.  Internally Flask makes29   sure that you always get the correct data for the active thread if you30   are in a multithreaded environment.3132   The request object is an instance of a :class:`~werkzeug.Request`33   subclass and provides all of the attributes Werkzeug defines.  This34   just shows a quick overview of the most important ones.3536   .. attribute:: form3738      A :class:`~werkzeug.MultiDict` with the parsed form data from `POST`39      or `PUT` requests.  Please keep in mind that file uploads will not40      end up here,  but instead in the :attr:`files` attribute.4142   .. attribute:: args4344      A :class:`~werkzeug.MultiDict` with the parsed contents of the query45      string.  (The part in the URL after the question mark).4647   .. attribute:: values4849      A :class:`~werkzeug.CombinedMultiDict` with the contents of both50      :attr:`form` and :attr:`args`.5152   .. attribute:: cookies5354      A :class:`dict` with the contents of all cookies transmitted with55      the request.5657   .. attribute:: stream5859      If the incoming form data was not encoded with a known encoding (for60      example it was transmitted as JSON) the data is stored unmodified in61      this stream for consumption.  For example to read the incoming62      request data as JSON, one can do the following::6364          json_body = simplejson.load(request.stream)6566   .. attribute:: files6768      A :class:`~werkzeug.MultiDict` with files uploaded as part of a69      `POST` or `PUT` request.  Each file is stored as70      :class:`~werkzeug.FileStorage` object.  It basically behaves like a71      standard file object you know from Python, with the difference that72      it also has a :meth:`~werkzeug.FileStorage.save` function that can73      store the file on the filesystem.7475   .. attribute:: environ7677      The underlying WSGI environment.7879   .. attribute:: method8081      The current request method (``POST``, ``GET`` etc.)8283   .. attribute:: path84   .. attribute:: script_root85   .. attribute:: url86   .. attribute:: base_url87   .. attribute:: url_root8889      Provides different ways to look at the current URL.  Imagine your90      application is listening on the following URL::9192          http://www.example.com/myapplication9394      And a user requests the following URL::9596          http://www.example.com/myapplication/page.html?x=y9798      In this case the values of the above mentioned attributes would be99      the following:100101      ============= ======================================================102      `path`        ``/page.html``103      `script_root` ``/myapplication``104      `url`         ``http://www.example.com/myapplication/page.html``105      `base_url`    ``http://www.example.com/myapplication/page.html?x=y``106      `root_url`    ``http://www.example.com/myapplication/``107      ============= ======================================================108109Response Objects110----------------111112.. autoclass:: flask.Response113   :members: set_cookie, data, mimetype114115   .. attribute:: headers116117      A :class:`Headers` object representing the response headers.118119   .. attribute:: status_code120121      The response status as integer.122123124Sessions125--------126127If you have the :attr:`Flask.secret_key` set you can use sessions in Flask128applications.  A session basically makes it possible to remember129information from one request to another.  The way Flask does this is by130using a signed cookie.  So the user can look at the session contents, but131not modify it unless he knows the secret key, so make sure to set that to132something complex and unguessable.133134To access the current session you can use the :class:`session` object:135136.. class:: session137138   The session object works pretty much like an ordinary dict, with the139   difference that it keeps track on modifications.140141   The following attributes are interesting:142143   .. attribute:: new144145      `True` if the session is new, `False` otherwise.146147   .. attribute:: modified148149      `True` if the session object detected a modification.  Be advised150      that modifications on mutable structures are not picked up151      automatically, in that situation you have to explicitly set the152      attribute to `True` yourself.  Here an example::153154          # this change is not picked up because a mutable object (here155          # a list) is changed.156          session['objects'].append(42)157          # so mark it as modified yourself158          session.modified = True159160161Application Globals162-------------------163164To share data that is valid for one request only from one function to165another, a global variable is not good enough because it would break in166threaded environments.  Flask provides you with a special object that167ensures it is only valid for the active request and that will return168different values for each request.  In a nutshell: it does the right169thing, like it does for :class:`request` and :class:`session`.170171.. data:: g172   173   Just store on this whatever you want.  For example a database174   connection or the user that is currently logged in.175176177Useful Functions and Classes178----------------------------179180.. autofunction:: url_for181182.. function:: abort(code)183184   Raises an :exc:`~werkzeug.exception.HTTPException` for the given185   status code.  For example to abort request handling with a page not186   found exception, you would call ``abort(404)``.187188   :param code: the HTTP error code.189190.. autofunction:: redirect191192.. autofunction:: escape193194.. autoclass:: Markup195   :members: escape, unescape, striptags196197Message Flashing198----------------199200.. autofunction:: flash201202.. autofunction:: get_flashed_messages203204Template Rendering205------------------206207.. autofunction:: render_template208209.. autofunction:: render_template_string