diff options
| author | Michael Merickel <michael@merickel.org> | 2015-02-11 12:32:26 -0600 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2015-02-11 12:32:35 -0600 |
| commit | 6e9e2dbb364f371b034b681dd44e6e6b831c5760 (patch) | |
| tree | b7d8c623706f5df330d9c72d4e1c9d84dd47b6cd /docs/narr/hooks.rst | |
| parent | 14126cae5cf308ed466ed3eea576094e9c2193b4 (diff) | |
| parent | 1dc1f28e1184960f5359c6c510d23a0e6e9dafe8 (diff) | |
| download | pyramid-6e9e2dbb364f371b034b681dd44e6e6b831c5760.tar.gz pyramid-6e9e2dbb364f371b034b681dd44e6e6b831c5760.tar.bz2 pyramid-6e9e2dbb364f371b034b681dd44e6e6b831c5760.zip | |
Merge branch 'master' into feature.py3-coverage
Diffstat (limited to 'docs/narr/hooks.rst')
| -rw-r--r-- | docs/narr/hooks.rst | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/docs/narr/hooks.rst b/docs/narr/hooks.rst index 4da36e730..4fd7670b9 100644 --- a/docs/narr/hooks.rst +++ b/docs/narr/hooks.rst @@ -349,6 +349,55 @@ We attach and cache an object named ``extra`` to the ``request`` object. the property .. index:: + single: response factory + +.. _changing_the_response_factory: + +Changing the Response Factory +------------------------------- + +.. versionadded:: 1.6 + +Whenever :app:`Pyramid` returns a response from a view it creates a +:term:`response` object. By default, an instance of the +:class:`pyramid.response.Response` class is created to represent the response +object. + +The factory that :app:`Pyramid` uses to create a response object instance can be +changed by passing a :class:`pyramid.interfaces.IResponseFactory` argument to +the constructor of the :term:`configurator`. This argument can be either a +callable or a :term:`dotted Python name` representing a callable. + +The factory takes a single positional argument, which is a :term:`Request` +object. The argument may be ``None``. + +.. code-block:: python + :linenos: + + from pyramid.response import Response + + class MyResponse(Response): + pass + + config = Configurator(response_factory=lambda r: MyResponse()) + +If you're doing imperative configuration, and you'd rather do it after you've +already constructed a :term:`configurator` it can also be registered via the +:meth:`pyramid.config.Configurator.set_response_factory` method: + +.. code-block:: python + :linenos: + + from pyramid.config import Configurator + from pyramid.response import Response + + class MyResponse(Response): + pass + + config = Configurator() + config.set_response_factory(lambda r: MyResponse()) + +.. index:: single: before render event single: adding renderer globals @@ -730,7 +779,7 @@ If you want to implement your own Response object instead of using the :class:`pyramid.response.Response` object in any capacity at all, you'll have to make sure the object implements every attribute and method outlined in :class:`pyramid.interfaces.IResponse` and you'll have to ensure that it uses -``zope.interface.implementer(IResponse)`` as a class decoratoror. +``zope.interface.implementer(IResponse)`` as a class decorator. .. code-block:: python :linenos: |
