Skip to content
Myr

Merge pull request #4847 from mhsmathew/patch-1

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Grey Li · committed by GitHub · parents e7c82f9, 5dfd212 · Browse files · 1 file+45 added−45 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+45 added−45 removed

  • other:+45 added−45 removed

1 file+45 added−45 removed

modified:docs/api.rst+45 added-45 removed
1 unmodified line
2===34.. module:: flask56This part of the documentation covers all the interfaces of Flask. For6This part of the documentation covers all the interfaces of Flask. For7parts where Flask depends on external libraries, we document the most8important right here and provide links to the canonical documentation.910
22 unmodified lines
3334.. attribute:: request3536 To access incoming request data, you can use the global `request`37 object. Flask parses incoming request data for you and gives you38 access to it through that global object. Internally Flask makes37 object. Flask parses incoming request data for you and gives you38 access to it through that global object. Internally Flask makes39 sure that you always get the correct data for the active thread if you40 are in a multithreaded environment.4142 This is a proxy. See :ref:`notes-on-proxies` for more information.42 This is a proxy. See :ref:`notes-on-proxies` for more information.4344 The request object is an instance of a :class:`~flask.Request`.4546
21 unmodified lines
6869 The session object works pretty much like an ordinary dict, with the70 difference that it keeps track of modifications.7172 This is a proxy. See :ref:`notes-on-proxies` for more information.72 This is a proxy. See :ref:`notes-on-proxies` for more information.7374 The following attributes are interesting:7576 .. attribute:: new
1 unmodified line
78 ``True`` if the session is new, ``False`` otherwise.7980 .. attribute:: modified8182 ``True`` if the session object detected a modification. Be advised82 ``True`` if the session object detected a modification. Be advised83 that modifications on mutable structures are not picked up84 automatically, in that situation you have to explicitly set the85 attribute to ``True`` yourself. Here an example::85 attribute to ``True`` yourself. Here an example::8687 # this change is not picked up because a mutable object (here88 # a list) is changed.89 session['objects'].append(42)
2 unmodified lines
9293 .. attribute:: permanent9495 If set to ``True`` the session lives for96 :attr:`~flask.Flask.permanent_session_lifetime` seconds. The97 default is 31 days. If set to ``False`` (which is the default) the96 :attr:`~flask.Flask.permanent_session_lifetime` seconds. The97 default is 31 days. If set to ``False`` (which is the default) the98 session will be deleted when the user closes the browser.99100101Session Interface
52 unmodified lines
154.. currentmodule:: flask155156To share data that is valid for one request only from one function to157another, a global variable is not good enough because it would break in158threaded environments. Flask provides you with a special object that158threaded environments. Flask provides you with a special object that159ensures it is only valid for the active request and that will return160different values for each request. In a nutshell: it does the right160different values for each request. In a nutshell: it does the right161thing, like it does for :class:`request` and :class:`session`.162163.. data:: g164
181 unmodified lines
346.. versionadded:: 0.6347348.. data:: signals.signals_available349350 ``True`` if the signaling system is available. This is the case350 ``True`` if the signaling system is available. This is the case351 when `blinker`_ is installed.352353The following signals exist in Flask:354355.. data:: template_rendered356357 This signal is sent when a template was successfully rendered. The357 This signal is sent when a template was successfully rendered. The358 signal is invoked with the instance of the template as `template`359 and the context as dictionary (named `context`).360361 Example subscriber::
25 unmodified lines
387388.. data:: request_started389390 This signal is sent when the request context is set up, before391 any request processing happens. Because the request context is already391 any request processing happens. Because the request context is already392 bound, the subscriber can access the request with the standard global393 proxies such as :class:`~flask.request`.394395 Example subscriber::
11 unmodified lines
407408 Example subscriber::409410 def log_response(sender, response, **extra):411 sender.logger.debug('Request context is about to close down. '411 sender.logger.debug('Request context is about to close down. '412 'Response: %s', response)413414 from flask import request_finished415 request_finished.connect(log_response, app)
28 unmodified lines
444 got_request_exception.connect(log_security_exception, app)445446.. data:: request_tearing_down447448 This signal is sent when the request is tearing down. This is always449 called, even if an exception is caused. Currently functions listening448 This signal is sent when the request is tearing down. This is always449 called, even if an exception is caused. Currently functions listening450 to this signal are called after the regular teardown handlers, but this451 is not something you can rely on.452453 Example subscriber::
9 unmodified lines
463 there was one.464465.. data:: appcontext_tearing_down466467 This signal is sent when the app context is tearing down. This is always468 called, even if an exception is caused. Currently functions listening467 This signal is sent when the app context is tearing down. This is always468 called, even if an exception is caused. Currently functions listening469 to this signal are called after the regular teardown handlers, but this470 is not something you can rely on.471472 Example subscriber::
8 unmodified lines
481 to the exception that caused the teardown if there was one.482483.. data:: appcontext_pushed484485 This signal is sent when an application context is pushed. The sender486 is the application. This is usually useful for unittests in order to487 temporarily hook in information. For instance it can be used to485 This signal is sent when an application context is pushed. The sender486 is the application. This is usually useful for unittests in order to487 temporarily hook in information. For instance it can be used to488 set a resource early onto the `g` object.489490 Example usage::491
18 unmodified lines
510 .. versionadded:: 0.10511512.. data:: appcontext_popped513514 This signal is sent when an application context is popped. The sender515 is the application. This usually falls in line with the514 This signal is sent when an application context is popped. The sender515 is the application. This usually falls in line with the516 :data:`appcontext_tearing_down` signal.517518 .. versionadded:: 0.10519520521.. data:: message_flashed522523 This signal is sent when the application is flashing a message. The523 This signal is sent when the application is flashing a message. The524 messages is sent as `message` keyword argument and the category as525 `category`.526527 Example subscriber::
9 unmodified lines
537538.. class:: signals.Namespace539540 An alias for :class:`blinker.base.Namespace` if blinker is available,541 otherwise a dummy class that creates fake signals. This class is541 otherwise a dummy class that creates fake signals. This class is542 available for Flask extensions that want to provide the same fallback543 system as Flask itself.544545 .. method:: signal(name, doc=None)
32 unmodified lines
5783. You can directly access the underlying Werkzeug routing system579 which is exposed as :attr:`flask.Flask.url_map`.580581Variable parts in the route can be specified with angular brackets582(``/user/<username>``). By default a variable part in the URL accepts any582(``/user/<username>``). By default a variable part in the URL accepts any583string without a slash however a different converter can be specified as584well by using ``<converter:name>``.585586Variable parts are passed to the view function as keyword arguments.
25 unmodified lines
612 def show_post(post_id):613 pass614615An important detail to keep in mind is how Flask deals with trailing616slashes. The idea is to keep each URL unique so the following rules616slashes. The idea is to keep each URL unique so the following rules617apply:6186191. If a rule ends with a slash and is requested without a slash by the620 user, the user is automatically redirected to the same page with a621 trailing slash attached.6222. If a rule does not end with a trailing slash and the user requests the623 page with a trailing slash, a 404 not found is raised.624625This is consistent with how web servers deal with static files. This625This is consistent with how web servers deal with static files. This626also makes it possible to use relative link targets safely.627628You can also define multiple rules for the same function. They have to be629unique however. Defaults can also be specified. Here for example is a628You can also define multiple rules for the same function. They have to be629unique however. Defaults can also be specified. Here for example is a630definition for a URL that accepts an optional page::631632 @app.route('/users/', defaults={'page': 1})633 @app.route('/users/page/<int:page>')
14 unmodified lines
648 def region(id):649 pass650651Here are the parameters that :meth:`~flask.Flask.route` and652:meth:`~flask.Flask.add_url_rule` accept. The only difference is that652:meth:`~flask.Flask.add_url_rule` accept. The only difference is that653with the route parameter the view function is defined with the decorator654instead of the `view_func` parameter.655656=============== ==========================================================657`rule` the URL rule as string658`endpoint` the endpoint for the registered URL rule. Flask itself658`endpoint` the endpoint for the registered URL rule. Flask itself659 assumes that the name of the view function is the name660 of the endpoint if not explicitly stated.661`view_func` the function to call when serving a request to the662 provided endpoint. If this is not provided one can662 provided endpoint. If this is not provided one can663 specify the function later by storing it in the664 :attr:`~flask.Flask.view_functions` dictionary with the665 endpoint as key.666`defaults` A dictionary with defaults for this rule. See the666`defaults` A dictionary with defaults for this rule. See the667 example above for how defaults work.668`subdomain` specifies the rule for the subdomain in case subdomain669 matching is in use. If not specified the default669 matching is in use. If not specified the default670 subdomain is assumed.671`**options` the options to be forwarded to the underlying672 :class:`~werkzeug.routing.Rule` object. A change to673 Werkzeug is handling of method options. methods is a list672 :class:`~werkzeug.routing.Rule` object. A change to673 Werkzeug is handling of method options. methods is a list674 of methods this rule should be limited to (``GET``, ``POST``675 etc.). By default a rule just listens for ``GET`` (and676 implicitly ``HEAD``). Starting with Flask 0.6, ``OPTIONS`` is675 etc.). By default a rule just listens for ``GET`` (and676 implicitly ``HEAD``). Starting with Flask 0.6, ``OPTIONS`` is677 implicitly added and handled by the standard request678 handling. They have to be specified as keyword arguments.678 handling. They have to be specified as keyword arguments.679=============== ==========================================================680681682View Function Options
3 unmodified lines
686customize behavior the view function would normally not have control over.687The following attributes can be provided optionally to either override688some defaults to :meth:`~flask.Flask.add_url_rule` or general behavior:689690- `__name__`: The name of a function is by default used as endpoint. If691 endpoint is provided explicitly this value is used. Additionally this690- `__name__`: The name of a function is by default used as endpoint. If691 endpoint is provided explicitly this value is used. Additionally this692 will be prefixed with the name of the blueprint by default which693 cannot be customized from the function itself.694695- `methods`: If methods are not provided when the URL rule is added,696 Flask will look on the view function object itself if a `methods`697 attribute exists. If it does, it will pull the information for the697 attribute exists. If it does, it will pull the information for the698 methods from there.699700- `provide_automatic_options`: if this attribute is set Flask will701 either force enable or disable the automatic implementation of the702 HTTP ``OPTIONS`` response. This can be useful when working with702 HTTP ``OPTIONS`` response. This can be useful when working with703 decorators that want to customize the ``OPTIONS`` response on a per-view704 basis.705706- `required_methods`: if this attribute is set, Flask will always add
44 unmodified lines