Skip to content
Myr

api.rst

This file

Path

docs/api.rst

351 lines · 10.2 KiB

Last changed by

Documented send_from_directoryArmin Ronacherbefore adoptionc34b03e

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

Documented send_from_directoryArmin Ronacherbefore adoptionc34b03e

lines 233–234why, one step back
233.. autofunction:: send_from_directory
234

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

lines 235–288why, one step back
235.. autofunction:: escape
236237.. autoclass:: Markup238 :members: escape, unescape, striptags239240Message Flashing241----------------242243.. autofunction:: flash244245.. autofunction:: get_flashed_messages246247Returning JSON248--------------249250.. autofunction:: jsonify251252.. data:: json253254 If JSON support is picked up, this will be the module that Flask is255 using to parse and serialize JSON. So instead of doing this yourself::256257 try:258 import simplejson as json259 except ImportError:260 import json261262 You can instead just do this::263264 from flask import json265266 For usage examples, read the :mod:`json` documentation.267268 The :func:`~json.dumps` function of this json module is also available269 as filter called ``|tojson`` in Jinja2. Note that inside `script`270 tags no escaping must take place, so make sure to disable escaping271 with ``|safe`` if you intend to use it inside `script` tags:272273 .. sourcecode:: html+jinja274275 <script type=text/javascript>276 doSomethingWith({{ user.username|tojson|safe }});277 </script>278279 Note that the ``|tojson`` filter escapes forward slashes properly.280281Template Rendering282------------------283284.. autofunction:: render_template285286.. autofunction:: render_template_string287288.. autofunction:: get_template_attribute

Improved configuration support.Armin Ronacherbefore adoption4d16486

lines 289–293why, one step back
289
290Configuration291-------------292293.. autoclass:: Config

Added chapter about configArmin Ronacherbefore adoption182ee31

line 294why, one step back
294 :members:

Documented _request_ctx_stack. This fixes #57Armin Ronacherbefore adoptiona6617f4

lines 295–304why, one step back
295
296Useful Internals297----------------298299.. data:: _request_ctx_stack300301 The internal :class:`~werkzeug.LocalStack` that is used to implement302 all the context local objects used in Flask. This is a documented303 instance and can be used by extensions and application code but the304 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 305why, one step back
305

improved _request_ctx_stack docsArmin Ronacherbefore adoption9373a71

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

Fixed an rst syntax errorArmin Ronacherbefore adoption881aa3a

line 307why, one step back
307 stack:

improved _request_ctx_stack docsArmin Ronacherbefore adoption9373a71

lines 308–335why, one step back
308
309 `app`310 the active Flask application.311312 `url_adapter`313 the URL adapter that was used to match the request.314315 `request`316 the current request object.317318 `session`319 the active session object.320321 `g`322 an object with all the attributes of the :data:`flask.g` object.323324 `flashes`325 an internal cache for the flashed messages.326327 Example usage::328329 from flask import _request_ctx_stack330331 def get_session():332 ctx = _request_ctx_stack.top333 if ctx is not None:334 return ctx.session335

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 336–351why, one step back
336 .. versionchanged:: 0.4
337338 The request context is automatically popped at the end of the request339 for you. In debug mode the request context is kept around if340 exceptions happen so that interactive debuggers have a chance to341 introspect the data. With 0.4 this can also be forced for requests342 that did not fail and outside of `DEBUG` mode. By setting343 ``'flask._preserve_context'`` to `True` on the WSGI environment the344 context will not pop itself at the end of the request. This is used by345 the :meth:`~flask.Flask.test_client` for example to implement the346 deferred cleanup functionality.347348 You might find this helpful for unittests where you need the349 information from the context local around for a little longer. Make350 sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in351 that situation, otherwise your unittests will leak memory.