Skip to content
Myr

api.rst

This file

Path

docs/api.rst

349 lines · 10.2 KiB

Last changed by

Fixed an rst syntax errorArmin Ronacherbefore adoption881aa3a

History of this file

Take it

The walk stopped after 200 commits: lines older than 5acc491 point at it.

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 1–17why, one step back
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:

Documentation update for the flask module support.Armin Ronacherbefore adoptionc0a9f5d

lines 18–26why, one step back
18 :inherited-members:
192021Module Objects22--------------2324.. autoclass:: Module25 :members:26 :inherited-members:

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 27–71why, one step back
27
28Incoming Request Data29---------------------3031.. autoclass:: Request3233.. class:: request3435 To access incoming request data, you can use the global `request`36 object. Flask parses incoming request data for you and gives you37 access to it through that global object. Internally Flask makes38 sure that you always get the correct data for the active thread if you39 are in a multithreaded environment.4041 The request object is an instance of a :class:`~werkzeug.Request`42 subclass and provides all of the attributes Werkzeug defines. This43 just shows a quick overview of the most important ones.4445 .. attribute:: form4647 A :class:`~werkzeug.MultiDict` with the parsed form data from `POST`48 or `PUT` requests. Please keep in mind that file uploads will not49 end up here, but instead in the :attr:`files` attribute.5051 .. attribute:: args5253 A :class:`~werkzeug.MultiDict` with the parsed contents of the query54 string. (The part in the URL after the question mark).5556 .. attribute:: values5758 A :class:`~werkzeug.CombinedMultiDict` with the contents of both59 :attr:`form` and :attr:`args`.6061 .. attribute:: cookies6263 A :class:`dict` with the contents of all cookies transmitted with64 the request.6566 .. attribute:: stream6768 If the incoming form data was not encoded with a known mimetype69 the data is stored unmodified in this stream for consumption. Most70 of the time it is a better idea to use :attr:`data` which will give71 you that data as a string. The stream only returns the data once.

More typo fixes.Georg Brandlbefore adoptiona224fec

line 72why, one step back
72

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 73–115why, one step back
73 .. attribute:: data
7475 Contains the incoming request data as string in case it came with76 a mimetype Flask does not handle.7778 .. attribute:: files7980 A :class:`~werkzeug.MultiDict` with files uploaded as part of a81 `POST` or `PUT` request. Each file is stored as82 :class:`~werkzeug.FileStorage` object. It basically behaves like a83 standard file object you know from Python, with the difference that84 it also has a :meth:`~werkzeug.FileStorage.save` function that can85 store the file on the filesystem.8687 .. attribute:: environ8889 The underlying WSGI environment.9091 .. attribute:: method9293 The current request method (``POST``, ``GET`` etc.)9495 .. attribute:: path96 .. attribute:: script_root97 .. attribute:: url98 .. attribute:: base_url99 .. attribute:: url_root100101 Provides different ways to look at the current URL. Imagine your102 application is listening on the following URL::103104 http://www.example.com/myapplication105106 And a user requests the following URL::107108 http://www.example.com/myapplication/page.html?x=y109110 In this case the values of the above mentioned attributes would be111 the following:112113 ============= ======================================================114 `path` ``/page.html``115 `script_root` ``/myapplication``

Merge branch 'master' into module-supportArmin Ronacherbefore adoption7f83790

lines 116–117why, one step back
116 `base_url` ``http://www.example.com/myapplication/page.html``
117 `url` ``http://www.example.com/myapplication/page.html?x=y``

Documented some routing converter possibilities.Armin Ronacherbefore adoption95750b3

line 118why, one step back
118 `url_root` ``http://www.example.com/myapplication/``

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 119–185why, one step back
119 ============= ======================================================
120121 .. attribute:: is_xhr122123 `True` if the request was triggered via a JavaScript124 `XMLHttpRequest`. This only works with libraries that support the125 ``X-Requested-With`` header and set it to `XMLHttpRequest`.126 Libraries that do that are prototype, jQuery and Mochikit and127 probably some more.128129 .. attribute:: json130131 Contains the parsed body of the JSON request if the mimetype of132 the incoming data was `application/json`. This requires Python 2.6133 or an installed version of simplejson.134135Response Objects136----------------137138.. autoclass:: flask.Response139 :members: set_cookie, data, mimetype140141 .. attribute:: headers142143 A :class:`Headers` object representing the response headers.144145 .. attribute:: status_code146147 The response status as integer.148149150Sessions151--------152153If you have the :attr:`Flask.secret_key` set you can use sessions in Flask154applications. A session basically makes it possible to remember155information from one request to another. The way Flask does this is by156using a signed cookie. So the user can look at the session contents, but157not modify it unless he knows the secret key, so make sure to set that to158something complex and unguessable.159160To access the current session you can use the :class:`session` object:161162.. class:: session163164 The session object works pretty much like an ordinary dict, with the165 difference that it keeps track on modifications.166167 The following attributes are interesting:168169 .. attribute:: new170171 `True` if the session is new, `False` otherwise.172173 .. attribute:: modified174175 `True` if the session object detected a modification. Be advised176 that modifications on mutable structures are not picked up177 automatically, in that situation you have to explicitly set the178 attribute to `True` yourself. Here an example::179180 # this change is not picked up because a mutable object (here181 # a list) is changed.182 session['objects'].append(42)183 # so mark it as modified yourself184 session.modified = True185

Fixed merging confictArmin Ronacherbefore adoptioneeb0e94

lines 186–192why, one step back
186 .. attribute:: permanent
187188 If set to `True` the session life for189 :attr:`~flask.Flask.permanent_session_lifetime` seconds. The190 default is 31 days. If set to `False` (which is the default) the191 session will be deleted when the user closes the browser.192

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 193–204why, one step back
193
194Application Globals195-------------------196197To share data that is valid for one request only from one function to198another, a global variable is not good enough because it would break in199threaded environments. Flask provides you with a special object that200ensures it is only valid for the active request and that will return201different values for each request. In a nutshell: it does the right202thing, like it does for :class:`request` and :class:`session`.203204.. data:: g

Fix typos and remove unused import.florentxbefore adoption3c821a0

line 205why, one step back
205

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 206–212why, one step back
206 Just store on this whatever you want. For example a database
207 connection or the user that is currently logged in.208209210Useful Functions and Classes211----------------------------212

Fixed some documentation issues. This fixes #41.Armin Ronacherbefore adoption854e0e2

lines 213–218why, one step back
213.. data:: current_app
214215 Points to the application handling the request. This is useful for216 extensions that want to support multiple applications running side217 by side.218

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 219–230why, one step back
219.. autofunction:: url_for
220221.. function:: abort(code)222223 Raises an :exc:`~werkzeug.exception.HTTPException` for the given224 status code. For example to abort request handling with a page not225 found exception, you would call ``abort(404)``.226227 :param code: the HTTP error code.228229.. autofunction:: redirect230

Merge branch 'master' into module-supportArmin Ronacherbefore adoption9fa4f94

lines 231–232why, one step back
231.. autofunction:: send_file
232

Refactored the @route decorator to use the new add_url_rule method.cgrindsbefore adoption5acc491

lines 233–286why, one step back
233.. autofunction:: escape
234235.. autoclass:: Markup236 :members: escape, unescape, striptags237238Message Flashing239----------------240241.. autofunction:: flash242243.. autofunction:: get_flashed_messages244245Returning JSON246--------------247248.. autofunction:: jsonify249250.. data:: json251252 If JSON support is picked up, this will be the module that Flask is253 using to parse and serialize JSON. So instead of doing this yourself::254255 try:256 import simplejson as json257 except ImportError:258 import json259260 You can instead just do this::261262 from flask import json263264 For usage examples, read the :mod:`json` documentation.265266 The :func:`~json.dumps` function of this json module is also available267 as filter called ``|tojson`` in Jinja2. Note that inside `script`268 tags no escaping must take place, so make sure to disable escaping269 with ``|safe`` if you intend to use it inside `script` tags:270271 .. sourcecode:: html+jinja272273 <script type=text/javascript>274 doSomethingWith({{ user.username|tojson|safe }});275 </script>276277 Note that the ``|tojson`` filter escapes forward slashes properly.278279Template Rendering280------------------281282.. autofunction:: render_template283284.. autofunction:: render_template_string285286.. autofunction:: get_template_attribute

Improved configuration support.Armin Ronacherbefore adoption4d16486

lines 287–291why, one step back
287
288Configuration289-------------290291.. autoclass:: Config

Added chapter about configArmin Ronacherbefore adoption182ee31

line 292why, one step back
292 :members:

Documented _request_ctx_stack. This fixes #57Armin Ronacherbefore adoptiona6617f4

lines 293–302why, one step back
293
294Useful Internals295----------------296297.. data:: _request_ctx_stack298299 The internal :class:`~werkzeug.LocalStack` that is used to implement300 all the context local objects used in Flask. This is a documented301 instance and can be used by extensions and application code but the302 use is discouraged in general.

Added support for deferred context cleanup. test_client users can now access the context locals after the actual request if the client is used with a with-block. This fixes #59.Armin Ronacherbefore adoptionbc00fd1

line 303why, one step back
303

improved _request_ctx_stack docsArmin Ronacherbefore adoption9373a71

line 304why, one step back
304 The following attributes are always present on each layer of the

Fixed an rst syntax errorArmin Ronacherbefore adoption881aa3a

line 305why, one step back
305 stack:

improved _request_ctx_stack docsArmin Ronacherbefore adoption9373a71

lines 306–333why, one step back
306
307 `app`308 the active Flask application.309310 `url_adapter`311 the URL adapter that was used to match the request.312313 `request`314 the current request object.315316 `session`317 the active session object.318319 `g`320 an object with all the attributes of the :data:`flask.g` object.321322 `flashes`323 an internal cache for the flashed messages.324325 Example usage::326327 from flask import _request_ctx_stack328329 def get_session():330 ctx = _request_ctx_stack.top331 if ctx is not None:332 return ctx.session333

Added support for deferred context cleanup. test_client users can now access the context locals after the actual request if the client is used with a with-block. This fixes #59.Armin Ronacherbefore adoptionbc00fd1

lines 334–349why, one step back
334 .. versionchanged:: 0.4
335336 The request context is automatically popped at the end of the request337 for you. In debug mode the request context is kept around if338 exceptions happen so that interactive debuggers have a chance to339 introspect the data. With 0.4 this can also be forced for requests340 that did not fail and outside of `DEBUG` mode. By setting341 ``'flask._preserve_context'`` to `True` on the WSGI environment the342 context will not pop itself at the end of the request. This is used by343 the :meth:`~flask.Flask.test_client` for example to implement the344 deferred cleanup functionality.345346 You might find this helpful for unittests where you need the347 information from the context local around for a little longer. Make348 sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in349 that situation, otherwise your unittests will leak memory.