2011-06-11 21:48:46 +00:00
|
|
|
``tornado.web`` --- ``RequestHandler`` and ``Application`` classes
|
|
|
|
==================================================================
|
2011-06-09 06:58:58 +00:00
|
|
|
|
|
|
|
.. automodule:: tornado.web
|
2011-06-09 08:15:56 +00:00
|
|
|
|
|
|
|
Request handlers
|
|
|
|
----------------
|
|
|
|
.. autoclass:: RequestHandler
|
|
|
|
|
2011-06-11 21:35:54 +00:00
|
|
|
Entry points
|
|
|
|
^^^^^^^^^^^^
|
2012-08-27 00:43:38 +00:00
|
|
|
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.initialize
|
|
|
|
.. automethod:: RequestHandler.prepare
|
2012-01-23 01:52:43 +00:00
|
|
|
.. automethod:: RequestHandler.on_finish
|
2011-06-11 21:35:54 +00:00
|
|
|
|
2013-04-18 14:40:20 +00:00
|
|
|
.. _verbs:
|
|
|
|
|
|
|
|
Implement any of the following methods (collectively known as the
|
|
|
|
HTTP verb methods) to handle the corresponding HTTP method.
|
2013-05-12 23:34:12 +00:00
|
|
|
These methods can be made asynchronous with one of the following
|
|
|
|
decorators: `.gen.coroutine`, `.return_future`, or `asynchronous`.
|
2011-06-11 21:35:54 +00:00
|
|
|
|
|
|
|
.. automethod:: RequestHandler.get
|
|
|
|
.. automethod:: RequestHandler.post
|
|
|
|
.. automethod:: RequestHandler.put
|
|
|
|
.. automethod:: RequestHandler.delete
|
|
|
|
.. automethod:: RequestHandler.head
|
|
|
|
.. automethod:: RequestHandler.options
|
|
|
|
|
|
|
|
Input
|
|
|
|
^^^^^
|
|
|
|
|
|
|
|
.. automethod:: RequestHandler.get_argument
|
|
|
|
.. automethod:: RequestHandler.get_arguments
|
2013-10-05 16:45:45 +00:00
|
|
|
.. automethod:: RequestHandler.get_query_argument
|
|
|
|
.. automethod:: RequestHandler.get_query_arguments
|
|
|
|
.. automethod:: RequestHandler.get_body_argument
|
|
|
|
.. automethod:: RequestHandler.get_body_arguments
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.decode_argument
|
2011-06-17 05:36:33 +00:00
|
|
|
.. attribute:: RequestHandler.request
|
|
|
|
|
2014-02-23 20:57:18 +00:00
|
|
|
The `tornado.httputil.HTTPServerRequest` object containing additional
|
2011-06-17 05:36:33 +00:00
|
|
|
request parameters including e.g. headers and body data.
|
2011-06-11 21:35:54 +00:00
|
|
|
|
2012-12-02 18:37:54 +00:00
|
|
|
.. attribute:: RequestHandler.path_args
|
|
|
|
.. attribute:: RequestHandler.path_kwargs
|
|
|
|
|
2013-04-18 14:40:20 +00:00
|
|
|
The ``path_args`` and ``path_kwargs`` attributes contain the
|
|
|
|
positional and keyword arguments that are passed to the
|
|
|
|
:ref:`HTTP verb methods <verbs>`. These attributes are set
|
|
|
|
before those methods are called, so the values are available
|
|
|
|
during `prepare`.
|
2012-12-02 18:37:54 +00:00
|
|
|
|
2011-06-11 21:35:54 +00:00
|
|
|
Output
|
|
|
|
^^^^^^
|
|
|
|
|
|
|
|
.. automethod:: RequestHandler.set_status
|
|
|
|
.. automethod:: RequestHandler.set_header
|
2011-09-04 22:39:55 +00:00
|
|
|
.. automethod:: RequestHandler.add_header
|
2012-08-27 21:49:20 +00:00
|
|
|
.. automethod:: RequestHandler.clear_header
|
2011-07-04 17:17:32 +00:00
|
|
|
.. automethod:: RequestHandler.set_default_headers
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.write
|
|
|
|
.. automethod:: RequestHandler.flush
|
|
|
|
.. automethod:: RequestHandler.finish
|
|
|
|
.. automethod:: RequestHandler.render
|
|
|
|
.. automethod:: RequestHandler.render_string
|
2012-08-27 00:43:38 +00:00
|
|
|
.. automethod:: RequestHandler.get_template_namespace
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.redirect
|
|
|
|
.. automethod:: RequestHandler.send_error
|
2011-07-04 19:16:50 +00:00
|
|
|
.. automethod:: RequestHandler.write_error
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.clear
|
2014-04-27 03:56:16 +00:00
|
|
|
.. automethod:: RequestHandler.data_received
|
2011-06-11 21:35:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
Cookies
|
|
|
|
^^^^^^^
|
|
|
|
|
|
|
|
.. autoattribute:: RequestHandler.cookies
|
|
|
|
.. automethod:: RequestHandler.get_cookie
|
|
|
|
.. automethod:: RequestHandler.set_cookie
|
|
|
|
.. automethod:: RequestHandler.clear_cookie
|
|
|
|
.. automethod:: RequestHandler.clear_all_cookies
|
|
|
|
.. automethod:: RequestHandler.get_secure_cookie
|
|
|
|
.. automethod:: RequestHandler.set_secure_cookie
|
|
|
|
.. automethod:: RequestHandler.create_signed_value
|
2014-05-06 01:58:22 +00:00
|
|
|
.. autodata:: MIN_SUPPORTED_SIGNED_VALUE_VERSION
|
|
|
|
.. autodata:: MAX_SUPPORTED_SIGNED_VALUE_VERSION
|
|
|
|
.. autodata:: DEFAULT_SIGNED_VALUE_VERSION
|
|
|
|
.. autodata:: DEFAULT_SIGNED_VALUE_MIN_VERSION
|
2011-06-11 21:35:54 +00:00
|
|
|
|
|
|
|
Other
|
|
|
|
^^^^^
|
|
|
|
|
2011-06-17 05:36:33 +00:00
|
|
|
.. attribute:: RequestHandler.application
|
|
|
|
|
|
|
|
The `Application` object serving this request
|
|
|
|
|
2013-12-31 22:50:12 +00:00
|
|
|
.. automethod:: RequestHandler.check_etag_header
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.check_xsrf_cookie
|
|
|
|
.. automethod:: RequestHandler.compute_etag
|
2012-08-27 22:56:34 +00:00
|
|
|
.. automethod:: RequestHandler.create_template_loader
|
2014-06-30 16:59:58 +00:00
|
|
|
.. autoattribute:: RequestHandler.current_user
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.get_browser_locale
|
|
|
|
.. automethod:: RequestHandler.get_current_user
|
|
|
|
.. automethod:: RequestHandler.get_login_url
|
|
|
|
.. automethod:: RequestHandler.get_status
|
|
|
|
.. automethod:: RequestHandler.get_template_path
|
|
|
|
.. automethod:: RequestHandler.get_user_locale
|
2013-05-12 22:08:23 +00:00
|
|
|
.. automethod:: RequestHandler.log_exception
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.on_connection_close
|
|
|
|
.. automethod:: RequestHandler.require_setting
|
2011-06-19 18:23:53 +00:00
|
|
|
.. automethod:: RequestHandler.reverse_url
|
2013-12-31 22:50:12 +00:00
|
|
|
.. automethod:: RequestHandler.set_etag_header
|
2011-06-17 05:36:33 +00:00
|
|
|
.. autoattribute:: RequestHandler.settings
|
2011-06-11 21:35:54 +00:00
|
|
|
.. automethod:: RequestHandler.static_url
|
|
|
|
.. automethod:: RequestHandler.xsrf_form_html
|
2014-06-04 03:16:00 +00:00
|
|
|
.. autoattribute:: RequestHandler.xsrf_token
|
2011-06-09 18:03:49 +00:00
|
|
|
|
2011-06-09 08:15:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
Application configuration
|
|
|
|
-----------------------------
|
|
|
|
.. autoclass:: Application
|
2011-06-09 18:03:49 +00:00
|
|
|
:members:
|
2011-06-09 08:15:56 +00:00
|
|
|
|
2012-08-27 22:56:34 +00:00
|
|
|
.. attribute:: settings
|
|
|
|
|
|
|
|
Additional keyword arguments passed to the constructor are
|
|
|
|
saved in the `settings` dictionary, and are often referred to
|
|
|
|
in documentation as "application settings". Settings are
|
|
|
|
used to customize various aspects of Tornado (although in
|
|
|
|
some cases richer customization is possible by overriding
|
|
|
|
methods in a subclass of `RequestHandler`). Some
|
|
|
|
applications also like to use the `settings` dictionary as a
|
|
|
|
way to make application-specific settings available to
|
|
|
|
handlers without using global variables. Settings used in
|
|
|
|
Tornado are described below.
|
|
|
|
|
|
|
|
General settings:
|
|
|
|
|
2013-11-04 02:10:56 +00:00
|
|
|
* ``autoreload``: If ``True``, the server process will restart
|
|
|
|
when any source files change, as described in :ref:`debug-mode`.
|
|
|
|
This option is new in Tornado 3.2; previously this functionality
|
|
|
|
was controlled by the ``debug`` setting.
|
|
|
|
* ``debug``: Shorthand for several debug mode settings,
|
|
|
|
described in :ref:`debug-mode`. Setting ``debug=True`` is
|
|
|
|
equivalent to ``autoreload=True``, ``compiled_template_cache=False``,
|
|
|
|
``static_hash_cache=False``, ``serve_traceback=True``.
|
|
|
|
* ``default_handler_class`` and ``default_handler_args``:
|
|
|
|
This handler will be used if no other match is found;
|
|
|
|
use this to implement custom 404 pages (new in Tornado 3.2).
|
2014-07-01 03:28:10 +00:00
|
|
|
* ``compress_response``: If ``True``, responses in textual formats
|
|
|
|
will be compressed automatically. New in Tornado 4.0.
|
|
|
|
* ``gzip``: Deprecated alias for ``compress_response`` since
|
|
|
|
Tornado 4.0.
|
2012-08-27 22:56:34 +00:00
|
|
|
* ``log_function``: This function will be called at the end
|
|
|
|
of every request to log the result (with one argument, the
|
|
|
|
`RequestHandler` object). The default implementation
|
|
|
|
writes to the `logging` module's root logger. May also be
|
|
|
|
customized by overriding `Application.log_request`.
|
2013-11-04 02:10:56 +00:00
|
|
|
* ``serve_traceback``: If true, the default error page
|
|
|
|
will include the traceback of the error. This option is new in
|
|
|
|
Tornado 3.2; previously this functionality was controlled by
|
|
|
|
the ``debug`` setting.
|
2012-08-27 22:56:34 +00:00
|
|
|
* ``ui_modules`` and ``ui_methods``: May be set to a mapping
|
|
|
|
of `UIModule` or UI methods to be made available to templates.
|
|
|
|
May be set to a module, dictionary, or a list of modules
|
|
|
|
and/or dicts. See :ref:`ui-modules` for more details.
|
|
|
|
|
|
|
|
Authentication and security settings:
|
|
|
|
|
|
|
|
* ``cookie_secret``: Used by `RequestHandler.get_secure_cookie`
|
2013-03-16 05:21:53 +00:00
|
|
|
and `.set_secure_cookie` to sign cookies.
|
2012-08-27 22:56:34 +00:00
|
|
|
* ``login_url``: The `authenticated` decorator will redirect
|
|
|
|
to this url if the user is not logged in. Can be further
|
|
|
|
customized by overriding `RequestHandler.get_login_url`
|
|
|
|
* ``xsrf_cookies``: If true, :ref:`xsrf` will be enabled.
|
2014-06-04 03:16:00 +00:00
|
|
|
* ``xsrf_cookie_version``: Controls the version of new XSRF
|
|
|
|
cookies produced by this server. Should generally be left
|
|
|
|
at the default (which will always be the highest supported
|
|
|
|
version), but may be set to a lower value temporarily
|
|
|
|
during version transitions. New in Tornado 3.2.2, which
|
|
|
|
introduced XSRF cookie version 2.
|
2012-08-27 22:56:34 +00:00
|
|
|
* ``twitter_consumer_key``, ``twitter_consumer_secret``,
|
|
|
|
``friendfeed_consumer_key``, ``friendfeed_consumer_secret``,
|
|
|
|
``google_consumer_key``, ``google_consumer_secret``,
|
|
|
|
``facebook_api_key``, ``facebook_secret``: Used in the
|
|
|
|
`tornado.auth` module to authenticate to various APIs.
|
|
|
|
|
|
|
|
Template settings:
|
|
|
|
|
|
|
|
* ``autoescape``: Controls automatic escaping for templates.
|
|
|
|
May be set to ``None`` to disable escaping, or to the *name*
|
|
|
|
of a function that all output should be passed through.
|
|
|
|
Defaults to ``"xhtml_escape"``. Can be changed on a per-template
|
|
|
|
basis with the ``{% autoescape %}`` directive.
|
2013-11-04 02:10:56 +00:00
|
|
|
* ``compiled_template_cache``: Default is ``True``; if ``False``
|
|
|
|
templates will be recompiled on every request. This option
|
|
|
|
is new in Tornado 3.2; previously this functionality was controlled
|
|
|
|
by the ``debug`` setting.
|
2012-08-27 22:56:34 +00:00
|
|
|
* ``template_path``: Directory containing template files. Can be
|
|
|
|
further customized by overriding `RequestHandler.get_template_path`
|
|
|
|
* ``template_loader``: Assign to an instance of
|
|
|
|
`tornado.template.BaseLoader` to customize template loading.
|
|
|
|
If this setting is used the ``template_path`` and ``autoescape``
|
|
|
|
settings are ignored. Can be further customized by overriding
|
|
|
|
`RequestHandler.create_template_loader`.
|
|
|
|
|
|
|
|
Static file settings:
|
|
|
|
|
2013-11-04 02:10:56 +00:00
|
|
|
* ``static_hash_cache``: Default is ``True``; if ``False``
|
|
|
|
static urls will be recomputed on every request. This option
|
|
|
|
is new in Tornado 3.2; previously this functionality was controlled
|
|
|
|
by the ``debug`` setting.
|
2012-08-27 22:56:34 +00:00
|
|
|
* ``static_path``: Directory from which static files will be
|
|
|
|
served.
|
|
|
|
* ``static_url_prefix``: Url prefix for static files,
|
|
|
|
defaults to ``"/static/"``.
|
|
|
|
* ``static_handler_class``, ``static_handler_args``: May be set to
|
|
|
|
use a different handler for static files instead of the default
|
|
|
|
`tornado.web.StaticFileHandler`. ``static_handler_args``, if set,
|
|
|
|
should be a dictionary of keyword arguments to be passed to the
|
|
|
|
handler's ``initialize`` method.
|
|
|
|
|
2011-06-09 08:15:56 +00:00
|
|
|
.. autoclass:: URLSpec
|
|
|
|
|
|
|
|
The ``URLSpec`` class is also available under the name ``tornado.web.url``.
|
|
|
|
|
|
|
|
Decorators
|
|
|
|
----------
|
|
|
|
.. autofunction:: asynchronous
|
|
|
|
.. autofunction:: authenticated
|
|
|
|
.. autofunction:: addslash
|
|
|
|
.. autofunction:: removeslash
|
2014-04-27 03:56:16 +00:00
|
|
|
.. autofunction:: stream_request_body
|
2011-06-09 08:15:56 +00:00
|
|
|
|
|
|
|
Everything else
|
|
|
|
---------------
|
2011-06-09 18:03:49 +00:00
|
|
|
.. autoexception:: HTTPError
|
2014-06-19 13:28:45 +00:00
|
|
|
.. autoexception:: Finish
|
2013-05-12 16:47:27 +00:00
|
|
|
.. autoexception:: MissingArgumentError
|
2011-06-09 18:03:49 +00:00
|
|
|
.. autoclass:: UIModule
|
|
|
|
:members:
|
|
|
|
|
|
|
|
.. autoclass:: ErrorHandler
|
|
|
|
.. autoclass:: FallbackHandler
|
|
|
|
.. autoclass:: RedirectHandler
|
|
|
|
.. autoclass:: StaticFileHandler
|
|
|
|
:members:
|