api.rst
This file
Path
docs/api.rst
319 lines · 9.5 KiB
Last changed by
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
History of this fileTake it
The walk stopped after 200 commits: lines older than 6a3a046 point at it.
Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 1–17why, one step back1.. _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 back18 :inherited-members:192021Module Objects22--------------2324.. autoclass:: Module25 :members:26 :inherited-members:Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 27–30why, one step back2728Incoming Request Data29---------------------30Added testing documentationArmin Ronacherbefore adoption03148db
lines 31–32why, one step back31.. autoclass:: Request32Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 33–67why, one step back33.. 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:: stream67Added JSON Support and started working on jQuery docsArmin Ronacherbefore adoption6e2be6a
lines 68–71why, one step back68 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 back72Added JSON Support and started working on jQuery docsArmin Ronacherbefore adoption6e2be6a
line 73why, one step back73 .. attribute:: dataAnother doc improvementArmin Ronacherbefore adoption6a3a046
line 74why, one step back74Added JSON Support and started working on jQuery docsArmin Ronacherbefore adoption6e2be6a
lines 75–76why, one step back75 Contains the incoming request data as string in case it came with76 a mimetype Flask does not handle.Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 77–115why, one step back7778 .. 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 back116 `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 back118 `url_root` ``http://www.example.com/myapplication/``Another doc improvementArmin Ronacherbefore adoption6a3a046
line 119why, one step back119 ============= ======================================================Added testing documentationArmin Ronacherbefore adoption03148db
line 120why, one step back120Added JSON Support and started working on jQuery docsArmin Ronacherbefore adoption6e2be6a
lines 121–134why, one step back121 .. 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.134Added testing documentationArmin Ronacherbefore adoption03148db
lines 135–147why, one step back135Response 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.Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 148–185why, one step back148149150Sessions151--------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 = True185Fixed merging confictArmin Ronacherbefore adoptioneeb0e94
lines 186–192why, one step back186 .. attribute:: permanent187188 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.192Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 193–204why, one step back193194Application 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:: gFix typos and remove unused import.florentxbefore adoption3c821a0
line 205why, one step back205Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 206–212why, one step back206 Just store on this whatever you want. For example a database207 connection or the user that is currently logged in.208209210Useful Functions and Classes211----------------------------212Fixed some documentation issues. This fixes #41.Armin Ronacherbefore adoption854e0e2
lines 213–218why, one step back213.. data:: current_app214215 Points to the application handling the request. This is useful for216 extensions that want to support multiple applications running side217 by side.218Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 219–230why, one step back219.. autofunction:: url_for220221.. 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:: redirect230Merge branch 'master' into module-supportArmin Ronacherbefore adoption9fa4f94
lines 231–232why, one step back231.. autofunction:: send_file232Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 233–243why, one step back233.. autofunction:: escape234235.. autoclass:: Markup236 :members: escape, unescape, striptags237238Message Flashing239----------------240241.. autofunction:: flash242243.. autofunction:: get_flashed_messagesAdded JSON Support and started working on jQuery docsArmin Ronacherbefore adoption6e2be6a
lines 244–275why, one step back244245Returning 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>Another doc improvementArmin Ronacherbefore adoption6a3a046
line 276why, one step back276Fixed a security problem caused by changed simplejson semantics.Armin Ronacherbefore adoptionade4905
lines 277–278why, one step back277 Note that the ``|tojson`` filter escapes forward slashes properly.278Another doc improvementArmin Ronacherbefore adoption6a3a046
lines 279–284why, one step back279Template Rendering280------------------281282.. autofunction:: render_template283284.. autofunction:: render_template_stringAdded support for macro pulling and documented certain design decisions.Armin Ronacherbefore adoption190059c
lines 285–286why, one step back285286.. autofunction:: get_template_attributeImproved configuration support.Armin Ronacherbefore adoption4d16486
lines 287–291why, one step back287288Configuration289-------------290291.. autoclass:: ConfigAdded chapter about configArmin Ronacherbefore adoption182ee31
line 292why, one step back292 :members:Documented _request_ctx_stack. This fixes #57Armin Ronacherbefore adoptiona6617f4
lines 293–302why, one step back293294Useful 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
lines 303–319why, one step back303304 .. versionchanged:: 0.4305306 The request context is automatically popped at the end of the request307 for you. In debug mode the request context is kept around if308 exceptions happen so that interactive debuggers have a chance to309 introspect the data. With 0.4 this can also be forced for requests310 that did not fail and outside of `DEBUG` mode. By setting311 ``'flask._preserve_context'`` to `True` on the WSGI environment the312 context will not pop itself at the end of the request. This is used by313 the :meth:`~flask.Flask.test_client` for example to implement the314 deferred cleanup functionality.315316 You might find this helpful for unittests where you need the317 information from the context local around for a little longer. Make318 sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in319 that situation, otherwise your unittests will leak memory.