api.rst
This file
Path
docs/api.rst
717 lines · 21.2 KiB
Last changed by
remove deprecated code (#5223)David Lordbefore adoption0e0e8dd
History of this fileTake it
The walk stopped after 200 commits: lines older than 1888df3 point at it.
Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 1–5why, one step back1API2===34.. module:: flask5Merge branch '2.2.x'David Lordbefore adoption066a35d
line 6why, one step back6This part of the documentation covers all the interfaces of Flask. ForMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 7–36why, one step back7parts where Flask depends on external libraries, we document the most8important right here and provide links to the canonical documentation.91011Application Object12------------------1314.. autoclass:: Flask15 :members:16 :inherited-members:171819Blueprint Objects20-----------------2122.. autoclass:: Blueprint23 :members:24 :inherited-members:2526Incoming Request Data27---------------------2829.. autoclass:: Request30 :members:31 :inherited-members:32 :exclude-members: json_module3334.. attribute:: request3536 To access incoming request data, you can use the global `request`Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 37–38why, one step back37 object. Flask parses incoming request data for you and gives you38 access to it through that global object. Internally Flask makesMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 39–41why, one step back39 sure that you always get the correct data for the active thread if you40 are in a multithreaded environment.41Merge branch '2.2.x'David Lordbefore adoption066a35d
line 42why, one step back42 This is a proxy. See :ref:`notes-on-proxies` for more information.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 43–71why, one step back4344 The request object is an instance of a :class:`~flask.Request`.454647Response Objects48----------------4950.. autoclass:: flask.Response51 :members:52 :inherited-members:53 :exclude-members: json_module5455Sessions56--------5758If you have set :attr:`Flask.secret_key` (or configured it from59:data:`SECRET_KEY`) you can use sessions in Flask applications. A session makes60it possible to remember information from one request to another. The way Flask61does this is by using a signed cookie. The user can look at the session62contents, but can't modify it unless they know the secret key, so make sure to63set that to something complex and unguessable.6465To access the current session you can use the :class:`session` object:6667.. class:: session6869 The session object works pretty much like an ordinary dict, with the70 difference that it keeps track of modifications.71Merge branch '2.2.x'David Lordbefore adoption066a35d
line 72why, one step back72 This is a proxy. See :ref:`notes-on-proxies` for more information.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 73–81why, one step back7374 The following attributes are interesting:7576 .. attribute:: new7778 ``True`` if the session is new, ``False`` otherwise.7980 .. attribute:: modified81Merge branch '2.2.x'David Lordbefore adoption066a35d
line 82why, one step back82 ``True`` if the session object detected a modification. Be advisedMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 83–84why, one step back83 that modifications on mutable structures are not picked up84 automatically, in that situation you have to explicitly set theMerge branch '2.2.x'David Lordbefore adoption066a35d
line 85why, one step back85 attribute to ``True`` yourself. Here an example::Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 86–95why, one step back8687 # this change is not picked up because a mutable object (here88 # a list) is changed.89 session['objects'].append(42)90 # so mark it as modified yourself91 session.modified = True9293 .. attribute:: permanent9495 If set to ``True`` the session lives forMerge branch '2.2.x'David Lordbefore adoption066a35d
lines 96–97why, one step back96 :attr:`~flask.Flask.permanent_session_lifetime` seconds. The97 default is 31 days. If set to ``False`` (which is the default) theMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 98–127why, one step back98 session will be deleted when the user closes the browser.99100101Session Interface102-----------------103104.. versionadded:: 0.8105106The session interface provides a simple way to replace the session107implementation that Flask is using.108109.. currentmodule:: flask.sessions110111.. autoclass:: SessionInterface112 :members:113114.. autoclass:: SecureCookieSessionInterface115 :members:116117.. autoclass:: SecureCookieSession118 :members:119120.. autoclass:: NullSession121 :members:122123.. autoclass:: SessionMixin124 :members:125126.. admonition:: Notice127Merge pull request #4722 from pallets/deprecate-config-attrsDavid Lordbefore adoptioncfd5783
lines 128–130why, one step back128 The :data:`PERMANENT_SESSION_LIFETIME` config can be an integer or ``timedelta``.129 The :attr:`~flask.Flask.permanent_session_lifetime` attribute is always a130 ``timedelta``.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 131–157why, one step back131132133Test Client134-----------135136.. currentmodule:: flask.testing137138.. autoclass:: FlaskClient139 :members:140141142Test CLI Runner143---------------144145.. currentmodule:: flask.testing146147.. autoclass:: FlaskCliRunner148 :members:149150151Application Globals152-------------------153154.. 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 inMerge branch '2.2.x'David Lordbefore adoption066a35d
line 158why, one step back158threaded environments. Flask provides you with a special object thatMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 159why, one step back159ensures it is only valid for the active request and that will returnMerge branch '2.2.x'David Lordbefore adoption066a35d
line 160why, one step back160different values for each request. In a nutshell: it does the rightMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 161–233why, one step back161thing, like it does for :class:`request` and :class:`session`.162163.. data:: g164165 A namespace object that can store data during an166 :doc:`application context </appcontext>`. This is an instance of167 :attr:`Flask.app_ctx_globals_class`, which defaults to168 :class:`ctx._AppCtxGlobals`.169170 This is a good place to store resources during a request. For171 example, a ``before_request`` function could load a user object from172 a session id, then set ``g.user`` to be used in the view function.173174 This is a proxy. See :ref:`notes-on-proxies` for more information.175176 .. versionchanged:: 0.10177 Bound to the application context instead of the request context.178179.. autoclass:: flask.ctx._AppCtxGlobals180 :members:181182183Useful Functions and Classes184----------------------------185186.. data:: current_app187188 A proxy to the application handling the current request. This is189 useful to access the application without needing to import it, or if190 it can't be imported, such as when using the application factory191 pattern or in blueprints and extensions.192193 This is only available when an194 :doc:`application context </appcontext>` is pushed. This happens195 automatically during requests and CLI commands. It can be controlled196 manually with :meth:`~flask.Flask.app_context`.197198 This is a proxy. See :ref:`notes-on-proxies` for more information.199200.. autofunction:: has_request_context201202.. autofunction:: copy_current_request_context203204.. autofunction:: has_app_context205206.. autofunction:: url_for207208.. autofunction:: abort209210.. autofunction:: redirect211212.. autofunction:: make_response213214.. autofunction:: after_this_request215216.. autofunction:: send_file217218.. autofunction:: send_from_directory219220221Message Flashing222----------------223224.. autofunction:: flash225226.. autofunction:: get_flashed_messages227228229JSON Support230------------231232.. module:: flask.json233Merge pull request #4692 from pallets/json-providerDavid Lordbefore adoption67310ab
lines 234–238why, one step back234Flask uses Python's built-in :mod:`json` module for handling JSON by235default. The JSON implementation can be changed by assigning a different236provider to :attr:`flask.Flask.json_provider_class` or237:attr:`flask.Flask.json`. The functions provided by ``flask.json`` will238use methods on ``app.json`` if an app context is active.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 239why, one step back239Merge pull request #4692 from pallets/json-providerDavid Lordbefore adoption67310ab
lines 240–242why, one step back240Jinja's ``|tojson`` filter is configured to use the app's JSON provider.241The filter marks the output with ``|safe``. Use it to render data inside242HTML ``<script>`` tags.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 243–246why, one step back243244.. sourcecode:: html+jinja245246 <script>Merge branch '2.2.x'David Lordbefore adoption7464e17
line 247why, one step back247 const names = {{ names|tojson }};Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 248–259why, one step back248 renderChart(names, {{ axis_data|tojson }});249 </script>250251.. autofunction:: jsonify252253.. autofunction:: dumps254255.. autofunction:: dump256257.. autofunction:: loads258259.. autofunction:: loadMerge pull request #4692 from pallets/json-providerDavid Lordbefore adoption67310ab
lines 260–267why, one step back260261.. autoclass:: flask.json.provider.JSONProvider262 :members:263 :member-order: bysource264265.. autoclass:: flask.json.provider.DefaultJSONProvider266 :members:267 :member-order: bysourceMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 268–280why, one step back268269.. automodule:: flask.json.tag270271272Template Rendering273------------------274275.. currentmodule:: flask276277.. autofunction:: render_template278279.. autofunction:: render_template_string280Merge pull request #4629 from pgjones/generateDavid Lordbefore adoptionab36542
lines 281–284why, one step back281.. autofunction:: stream_template282283.. autofunction:: stream_template_string284Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 285–304why, one step back285.. autofunction:: get_template_attribute286287Configuration288-------------289290.. autoclass:: Config291 :members:292293294Stream Helpers295--------------296297.. autofunction:: stream_with_context298299Useful Internals300----------------301302.. autoclass:: flask.ctx.RequestContext303 :members:304Merge pull request #4682 from pallets/refactor-context-stackDavid Lordbefore adoptioncbebdae
line 305why, one step back305.. data:: flask.globals.request_ctxMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 306why, one step back306Merge pull request #4682 from pallets/refactor-context-stackDavid Lordbefore adoptioncbebdae
lines 307–309why, one step back307 The current :class:`~flask.ctx.RequestContext`. If a request context308 is not active, accessing attributes on this proxy will raise a309 ``RuntimeError``.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 310why, one step back310Merge pull request #4682 from pallets/refactor-context-stackDavid Lordbefore adoptioncbebdae
lines 311–313why, one step back311 This is an internal object that is essential to how Flask handles312 requests. Accessing this should not be needed in most cases. Most313 likely you want :data:`request` and :data:`session` instead.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 314–317why, one step back314315.. autoclass:: flask.ctx.AppContext316 :members:317Merge pull request #4682 from pallets/refactor-context-stackDavid Lordbefore adoptioncbebdae
line 318why, one step back318.. data:: flask.globals.app_ctxMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 319why, one step back319Merge pull request #4682 from pallets/refactor-context-stackDavid Lordbefore adoptioncbebdae
lines 320–322why, one step back320 The current :class:`~flask.ctx.AppContext`. If an app context is not321 active, accessing attributes on this proxy will raise a322 ``RuntimeError``.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 323why, one step back323Merge pull request #4682 from pallets/refactor-context-stackDavid Lordbefore adoptioncbebdae
lines 324–326why, one step back324 This is an internal object that is essential to how Flask handles325 requests. Accessing this should not be needed in most cases. Most326 likely you want :data:`current_app` and :data:`g` instead.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 327–335why, one step back327328.. autoclass:: flask.blueprints.BlueprintSetupState329 :members:330331.. _core-signals-list:332333Signals334-------335Merge pull request #5058 from pallets/require-blinkerDavid Lordbefore adoptiona05c0c6
line 336why, one step back336Signals are provided by the `Blinker`_ library. See :doc:`signals` for an introduction.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 337why, one step back337Merge pull request #5058 from pallets/require-blinkerDavid Lordbefore adoptiona05c0c6
line 338why, one step back338.. _blinker: https://blinker.readthedocs.io/Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 339–341why, one step back339340.. data:: template_rendered341Merge branch '2.2.x'David Lordbefore adoption066a35d
line 342why, one step back342 This signal is sent when a template was successfully rendered. TheMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 343–375why, one step back343 signal is invoked with the instance of the template as `template`344 and the context as dictionary (named `context`).345346 Example subscriber::347348 def log_template_renders(sender, template, context, **extra):349 sender.logger.debug('Rendering template "%s" with context %s',350 template.name or 'string template',351 context)352353 from flask import template_rendered354 template_rendered.connect(log_template_renders, app)355356.. data:: flask.before_render_template357 :noindex:358359 This signal is sent before template rendering process. The360 signal is invoked with the instance of the template as `template`361 and the context as dictionary (named `context`).362363 Example subscriber::364365 def log_template_renders(sender, template, context, **extra):366 sender.logger.debug('Rendering template "%s" with context %s',367 template.name or 'string template',368 context)369370 from flask import before_render_template371 before_render_template.connect(log_template_renders, app)372373.. data:: request_started374375 This signal is sent when the request context is set up, beforeMerge branch '2.2.x'David Lordbefore adoption066a35d
line 376why, one step back376 any request processing happens. Because the request context is alreadyMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 377–395why, one step back377 bound, the subscriber can access the request with the standard global378 proxies such as :class:`~flask.request`.379380 Example subscriber::381382 def log_request(sender, **extra):383 sender.logger.debug('Request context is set up')384385 from flask import request_started386 request_started.connect(log_request, app)387388.. data:: request_finished389390 This signal is sent right before the response is sent to the client.391 It is passed the response to be sent named `response`.392393 Example subscriber::394395 def log_response(sender, response, **extra):Merge branch '2.2.x'David Lordbefore adoption066a35d
line 396why, one step back396 sender.logger.debug('Request context is about to close down. 'Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 397–432why, one step back397 'Response: %s', response)398399 from flask import request_finished400 request_finished.connect(log_response, app)401402.. data:: got_request_exception403404 This signal is sent when an unhandled exception happens during405 request processing, including when debugging. The exception is406 passed to the subscriber as ``exception``.407408 This signal is not sent for409 :exc:`~werkzeug.exceptions.HTTPException`, or other exceptions that410 have error handlers registered, unless the exception was raised from411 an error handler.412413 This example shows how to do some extra logging if a theoretical414 ``SecurityException`` was raised:415416 .. code-block:: python417418 from flask import got_request_exception419420 def log_security_exception(sender, exception, **extra):421 if not isinstance(exception, SecurityException):422 return423424 security_logger.exception(425 f"SecurityException at {request.url!r}",426 exc_info=exception,427 )428429 got_request_exception.connect(log_security_exception, app)430431.. data:: request_tearing_down432Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 433–434why, one step back433 This signal is sent when the request is tearing down. This is always434 called, even if an exception is caused. Currently functions listeningMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 435–451why, one step back435 to this signal are called after the regular teardown handlers, but this436 is not something you can rely on.437438 Example subscriber::439440 def close_db_connection(sender, **extra):441 session.close()442443 from flask import request_tearing_down444 request_tearing_down.connect(close_db_connection, app)445446 As of Flask 0.9, this will also be passed an `exc` keyword argument447 that has a reference to the exception that caused the teardown if448 there was one.449450.. data:: appcontext_tearing_down451Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 452–453why, one step back452 This signal is sent when the app context is tearing down. This is always453 called, even if an exception is caused. Currently functions listeningMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 454–469why, one step back454 to this signal are called after the regular teardown handlers, but this455 is not something you can rely on.456457 Example subscriber::458459 def close_db_connection(sender, **extra):460 session.close()461462 from flask import appcontext_tearing_down463 appcontext_tearing_down.connect(close_db_connection, app)464465 This will also be passed an `exc` keyword argument that has a reference466 to the exception that caused the teardown if there was one.467468.. data:: appcontext_pushed469Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 470–472why, one step back470 This signal is sent when an application context is pushed. The sender471 is the application. This is usually useful for unittests in order to472 temporarily hook in information. For instance it can be used toMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 473–498why, one step back473 set a resource early onto the `g` object.474475 Example usage::476477 from contextlib import contextmanager478 from flask import appcontext_pushed479480 @contextmanager481 def user_set(app, user):482 def handler(sender, **kwargs):483 g.user = user484 with appcontext_pushed.connected_to(handler, app):485 yield486487 And in the testcode::488489 def test_user_me(self):490 with user_set(app, 'john'):491 c = app.test_client()492 resp = c.get('/users/me')493 assert resp.data == 'username=john'494495 .. versionadded:: 0.10496497.. data:: appcontext_popped498Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 499–500why, one step back499 This signal is sent when an application context is popped. The sender500 is the application. This usually falls in line with theMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 501–506why, one step back501 :data:`appcontext_tearing_down` signal.502503 .. versionadded:: 0.10504505.. data:: message_flashed506Merge branch '2.2.x'David Lordbefore adoption066a35d
line 507why, one step back507 This signal is sent when the application is flashing a message. TheMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 508–548why, one step back508 messages is sent as `message` keyword argument and the category as509 `category`.510511 Example subscriber::512513 recorded = []514 def record(sender, message, category, **extra):515 recorded.append((message, category))516517 from flask import message_flashed518 message_flashed.connect(record, app)519520 .. versionadded:: 0.10521522523Class-Based Views524-----------------525526.. versionadded:: 0.7527528.. currentmodule:: None529530.. autoclass:: flask.views.View531 :members:532533.. autoclass:: flask.views.MethodView534 :members:535536.. _url-route-registrations:537538URL Route Registrations539-----------------------540541Generally there are three ways to define rules for the routing system:5425431. You can use the :meth:`flask.Flask.route` decorator.5442. You can use the :meth:`flask.Flask.add_url_rule` function.5453. You can directly access the underlying Werkzeug routing system546 which is exposed as :attr:`flask.Flask.url_map`.547548Variable parts in the route can be specified with angular bracketsMerge branch '2.2.x'David Lordbefore adoption066a35d
line 549why, one step back549(``/user/<username>``). By default a variable part in the URL accepts anyMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 550–582why, one step back550string without a slash however a different converter can be specified as551well by using ``<converter:name>``.552553Variable parts are passed to the view function as keyword arguments.554555The following converters are available:556557=========== ===============================================558`string` accepts any text without a slash (the default)559`int` accepts integers560`float` like `int` but for floating point values561`path` like the default but also accepts slashes562`any` matches one of the items provided563`uuid` accepts UUID strings564=========== ===============================================565566Custom converters can be defined using :attr:`flask.Flask.url_map`.567568Here are some examples::569570 @app.route('/')571 def index():572 pass573574 @app.route('/<username>')575 def show_user(username):576 pass577578 @app.route('/post/<int:post_id>')579 def show_post(post_id):580 pass581582An important detail to keep in mind is how Flask deals with trailingMerge branch '2.2.x'David Lordbefore adoption066a35d
line 583why, one step back583slashes. The idea is to keep each URL unique so the following rulesMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 584–591why, one step back584apply:5855861. If a rule ends with a slash and is requested without a slash by the587 user, the user is automatically redirected to the same page with a588 trailing slash attached.5892. If a rule does not end with a trailing slash and the user requests the590 page with a trailing slash, a 404 not found is raised.591Merge branch '2.2.x'David Lordbefore adoption066a35d
line 592why, one step back592This is consistent with how web servers deal with static files. ThisMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 593–594why, one step back593also makes it possible to use relative link targets safely.594Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 595–596why, one step back595You can also define multiple rules for the same function. They have to be596unique however. Defaults can also be specified. Here for example is aMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 597–618why, one step back597definition for a URL that accepts an optional page::598599 @app.route('/users/', defaults={'page': 1})600 @app.route('/users/page/<int:page>')601 def show_users(page):602 pass603604This specifies that ``/users/`` will be the URL for page one and605``/users/page/N`` will be the URL for page ``N``.606607If a URL contains a default value, it will be redirected to its simpler608form with a 301 redirect. In the above example, ``/users/page/1`` will609be redirected to ``/users/``. If your route handles ``GET`` and ``POST``610requests, make sure the default route only handles ``GET``, as redirects611can't preserve form data. ::612613 @app.route('/region/', defaults={'id': 1})614 @app.route('/region/<int:id>', methods=['GET', 'POST'])615 def region(id):616 pass617618Here are the parameters that :meth:`~flask.Flask.route` andMerge branch '2.2.x'David Lordbefore adoption066a35d
line 619why, one step back619:meth:`~flask.Flask.add_url_rule` accept. The only difference is thatMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 620–624why, one step back620with the route parameter the view function is defined with the decorator621instead of the `view_func` parameter.622623=============== ==========================================================624`rule` the URL rule as stringMerge branch '2.2.x'David Lordbefore adoption066a35d
line 625why, one step back625`endpoint` the endpoint for the registered URL rule. Flask itselfMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 626–628why, one step back626 assumes that the name of the view function is the name627 of the endpoint if not explicitly stated.628`view_func` the function to call when serving a request to theMerge branch '2.2.x'David Lordbefore adoption066a35d
line 629why, one step back629 provided endpoint. If this is not provided one canMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 630–632why, one step back630 specify the function later by storing it in the631 :attr:`~flask.Flask.view_functions` dictionary with the632 endpoint as key.Merge branch '2.2.x'David Lordbefore adoption066a35d
line 633why, one step back633`defaults` A dictionary with defaults for this rule. See theMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 634–635why, one step back634 example above for how defaults work.635`subdomain` specifies the rule for the subdomain in case subdomainMerge branch '2.2.x'David Lordbefore adoption066a35d
line 636why, one step back636 matching is in use. If not specified the defaultMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 637–638why, one step back637 subdomain is assumed.638`**options` the options to be forwarded to the underlyingMerge branch '2.2.x'David Lordbefore adoption066a35d
lines 639–640why, one step back639 :class:`~werkzeug.routing.Rule` object. A change to640 Werkzeug is handling of method options. methods is a listMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 641why, one step back641 of methods this rule should be limited to (``GET``, ``POST``Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 642–643why, one step back642 etc.). By default a rule just listens for ``GET`` (and643 implicitly ``HEAD``). Starting with Flask 0.6, ``OPTIONS`` isMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
line 644why, one step back644 implicitly added and handled by the standard requestMerge branch '2.2.x'David Lordbefore adoption066a35d
line 645why, one step back645 handling. They have to be specified as keyword arguments.Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 646–656why, one step back646=============== ==========================================================647648649View Function Options650---------------------651652For internal usage the view functions can have some attributes attached to653customize behavior the view function would normally not have control over.654The following attributes can be provided optionally to either override655some defaults to :meth:`~flask.Flask.add_url_rule` or general behavior:656Merge branch '2.2.x'David Lordbefore adoption066a35d
lines 657–658why, one step back657- `__name__`: The name of a function is by default used as endpoint. If658 endpoint is provided explicitly this value is used. Additionally thisMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 659–663why, one step back659 will be prefixed with the name of the blueprint by default which660 cannot be customized from the function itself.661662- `methods`: If methods are not provided when the URL rule is added,663 Flask will look on the view function object itself if a `methods`Merge branch '2.2.x'David Lordbefore adoption066a35d
line 664why, one step back664 attribute exists. If it does, it will pull the information for theMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 665–668why, one step back665 methods from there.666667- `provide_automatic_options`: if this attribute is set Flask will668 either force enable or disable the automatic implementation of theMerge branch '2.2.x'David Lordbefore adoption066a35d
line 669why, one step back669 HTTP ``OPTIONS`` response. This can be useful when working withMerge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3
lines 670–717why, one step back670 decorators that want to customize the ``OPTIONS`` response on a per-view671 basis.672673- `required_methods`: if this attribute is set, Flask will always add674 these methods when registering a URL rule even if the methods were675 explicitly overridden in the ``route()`` call.676677Full example::678679 def index():680 if request.method == 'OPTIONS':681 # custom options handling here682 ...683 return 'Hello World!'684 index.provide_automatic_options = False685 index.methods = ['GET', 'OPTIONS']686687 app.add_url_rule('/', index)688689.. versionadded:: 0.8690 The `provide_automatic_options` functionality was added.691692Command Line Interface693----------------------694695.. currentmodule:: flask.cli696697.. autoclass:: FlaskGroup698 :members:699700.. autoclass:: AppGroup701 :members:702703.. autoclass:: ScriptInfo704 :members:705706.. autofunction:: load_dotenv707708.. autofunction:: with_appcontext709710.. autofunction:: pass_script_info711712 Marks a function so that an instance of :class:`ScriptInfo` is passed713 as first argument to the click callback.714715.. autodata:: run_command716717.. autodata:: shell_command