From 64ab59817dedd7959b4b38d47403c86415f4a322 Mon Sep 17 00:00:00 2001 From: hankhank10 Date: Sun, 26 Jun 2022 10:14:40 +0100 Subject: [PATCH] show separate HTTP method route decorators in quickstart --- docs/quickstart.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index a6956c32..ed345aad 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -390,6 +390,24 @@ of the :meth:`~flask.Flask.route` decorator to handle different HTTP methods. else: return show_the_login_form() +The example above keeps all methods for the route within one function, +which can be useful if each part uses some common data. + +You can also separate views for different methods into different +functions. Flask provides a shortcut for decorating such routes with +:meth:`~flask.Flask.get`, :meth:`~flask.Flask.post`, etc. for each +common HTTP method. + +.. code-block:: python + + @app.get('/login') + def login_get(): + return show_the_login_form() + + @app.post('/login') + def login_post(): + return do_the_login() + If ``GET`` is present, Flask automatically adds support for the ``HEAD`` method and handles ``HEAD`` requests according to the `HTTP RFC`_. Likewise, ``OPTIONS`` is automatically implemented for you.