.. _views_chapter: Views ===== The primary job of any :mod:`pyramid` application is is to find and invoke a :term:`view callable` when a :term:`request` reaches the application. View callables are bits of code written by you -- the application developer -- which do something interesting in response to a request made to your application. .. note:: A :mod:`pyramid` :term:`view callable` is often referred to in conversational shorthand as a :term:`view`. In this documentation, however, we need to use less ambiguous terminology because there are significant differences between view *configuration*, the code that implements a view *callable*, and the process of view *lookup*. The chapter named :ref:`contextfinding_chapter` describes how, using information from the :term:`request`, a :term:`context` and a :term:`view name` are computed. But neither the context nor the view name found are very useful unless those elements can eventually be mapped to a :term:`view callable`. The job of actually locating and invoking the "best" :term:`view callable` is the job of the :term:`view lookup` subsystem. The view lookup subsystem compares information supplied by :term:`context finding` against :term:`view configuration` statements made by the developer stored in the :term:`application registry` to choose the most appropriate view callable for a specific request. This chapter provides documentation detailing the process of creating view callables, documentation about performing view configuration, and a detailed explanation of view lookup. View Callables -------------- No matter how a view callable is eventually found, all view callables used by :mod:`pyramid` must be constructed in the same way, and must return the same kind of return value. Most view callables accept a single argument named ``request``. This argument represents a :mod:`pyramid` :term:`Request` object. A request object encapsulates a WSGI environment as represented to :mod:`pyramid` by the upstream :term:`WSGI` server. A view callable may always return a :mod:`Pyramid` :term:`Response` object directly. It may optionally return another arbitrary non-Response value: if a view callable returns a non-Response result, the result must be converted into a response by the :term:`renderer` associated with the :term:`view configuration` for the view. View callables can be functions, instances, or classes. View callables can optionally be defined with an alternate calling convention. .. index:: single: view calling convention single: view function .. _function_as_view: Defining a View Callable as a Function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The easiest way to define a view callable is to create a function that accepts a single argument named ``request`` and which returns a :term:`Response` object. For example, this is a "hello world" view callable implemented as a function: .. code-block:: python :linenos: from pyramid.response import Response def hello_world(request): return Response('Hello world!') .. index:: single: view calling convention single: view class .. _class_as_view: Defining a View Callable as a Class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A view callable may also be a class instead of a function. When a view callable is a class, the calling semantics are slightly different than when it is a function or another non-class callable. When a view callable is a class, the class' ``__init__`` is called with a ``request`` parameter. As a result, an instance of the class is created. Subsequently, that instance's ``__call__`` method is invoked with no parameters. Views defined as classes must have the following traits: - an ``__init__`` method that accepts a ``request`` as its sole positional argument or an ``__init__`` method that accepts two arguments: ``request`` and ``context`` as per :ref:`request_and_context_view_definitions`. - a ``__call__`` method that accepts no parameters and which returns a response. For example: .. code-block:: python :linenos: from pyramid.response import Response class MyView(object): def __init__(self, request): self.request = request def __call__(self): return Response('hello') The request object passed to ``__init__`` is the same type of request object described in :ref:`function_as_view`. If you'd like to use a different attribute than ``__call__`` to represent the method expected to return a response, you can use an ``attr`` value as part of view configuration. See :ref:`view_configuration_parameters`. .. index:: single: view calling convention .. _request_and_context_view_definitions: Context-And-Request View Callable Definitions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Usually, view callables are defined to accept only a single argument: ``request``. However, view callables may alternately be defined as classes or functions (or any callable) that accept *two* positional arguments: a :term:`context` as the first argument and a :term:`request` as the second argument. The :term:`context` and :term:`request` arguments passed to a view function defined in this style can be defined as follows: context An instance of a :term:`context` found via graph :term:`traversal` or :term:`URL dispatch`. If the context is found via traversal, it will be a :term:`model` object. request A :mod:`pyramid` Request object representing the current WSGI request. The following types work as view callables in this style: #. Functions that accept two arguments: ``context``, and ``request``, e.g.: .. code-block:: python :linenos: from pyramid.response import Response def view(context, request): return Response('OK') #. Classes that have an ``__init__`` method that accepts ``context, request`` and a ``__call__`` which accepts no arguments, e.g.: .. code-block:: python :linenos: from pyramid.response import Response class view(object): def __init__(self, context, request): self.context = context self.request = request def __call__(self): return Response('OK') #. Arbitrary callables that have a ``__call__`` method that accepts ``context, request``, e.g.: .. code-block:: python :linenos: from pyramid.response import Response class View(object): def __call__(self, context, request): return Response('OK') view = View() # this is the view callable This style of calling convention is most useful for :term:`traversal` based applications, where the context object is frequently used within the view callable code itself. No matter which view calling convention is used, the view code always has access to the context via ``request.context``. .. index:: single: view response single: response .. _the_response: View Callable Responses ~~~~~~~~~~~~~~~~~~~~~~~ A view callable may always return an object that implements the :mod:`pyramid` :term:`Response` interface. The easiest way to return something that implements the :term:`Response` interface is to return a :class:`pyramid.response.Response` object instance directly. For example: .. code-block:: python :linenos: from pyramid.response import Response def view(request): return Response('OK') You don't need to always use :class:`pyramid.response.Response` to represent a response. :mod:`pyramid` provides a range of different "exception" classes which can act as response objects too. For example, an instance of the class :class:`pyramid.httpexceptions.HTTPFound` is also a valid response object (see :ref:`http_redirect`). A view can actually return any object that has the following attributes (these attributes form the notional "Pyramid Response interface"): status The HTTP status code (including the name) for the response as a string. E.g. ``200 OK`` or ``401 Unauthorized``. headerlist A sequence of tuples representing the list of headers that should be set in the response. E.g. ``[('Content-Type', 'text/html'), ('Content-Length', '412')]`` app_iter An iterable representing the body of the response. This can be a list, e.g. ``['
Hello world!']`` or it can be a file-like object, or any other sort of iterable. Furthermore, a view needn't *always* return a Response object. If a view happens to return something which does not implement the Pyramid Response interface, :mod:`pyramid` will attempt to use a :term:`renderer` to construct a response. For example: .. code-block:: python :linenos: from pyramid.response import Response from pyramid.view import view_config @view_config(renderer='json') def hello_world(request): return {'content':'Hello!'} The above example returns a *dictionary* from the view callable. A dictionary does not implement the Pyramid response interface, so you might believe that this example would fail. However, since a ``renderer`` is associated with the view callable through its :term:`view configuration` (in this case, using a ``renderer`` argument passed to :func:`pyramid.view.view_config`), if the view does *not* return a Response object, the renderer will attempt to convert the result of the view to a response on the developer's behalf. Of course, if no renderer is associated with a view's configuration, returning anything except an object which implements the Response interface will result in an error. And, if a renderer *is* used, whatever is returned by the view must be compatible with the particular kind of renderer used, or an error may occur during view invocation. One exception exists: it is *always* OK to return a Response object, even when a ``renderer`` is configured. If a view callable returns a response object from a view that is configured with a renderer, the renderer is bypassed entirely. Various types of renderers exist, including serialization renderers and renderers which use templating systems. See also :ref:`views_which_use_a_renderer`. .. index:: single: view http redirect single: http redirect (from a view) .. _http_redirect: Using a View Callable to Do A HTTP Redirect ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can issue an HTTP redirect from within a view by returning a particular kind of response. .. code-block:: python :linenos: from pyramid.httpexceptions import HTTPFound def myview(request): return HTTPFound(location='http://example.com') All exception types from the :mod:`pyramid.httpexceptions` module implement the :term:`Response` interface; any can be returned as the response from a view. See :mod:`pyramid.httpexceptions` for the documentation for the ``HTTPFound`` exception; it also includes other response types that imply other HTTP response codes, such as ``HTTPUnauthorized`` for ``401 Unauthorized``. .. index:: single: renderer single: view renderer .. _views_which_use_a_renderer: Writing View Callables Which Use a Renderer ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ View callables needn't always return a Response object. Instead, they may return an arbitrary Python object, with the expectation that a :term:`renderer` will convert that object into a response instance on behalf of the developer. Some renderers use a templating system; other renderers use object serialization techniques. If you do not define a ``renderer`` attribute in :term:`view configuration` for an associated :term:`view callable`, no renderer is associated with the view. In such a configuration, an error is raised when a view callable does not return an object which implements the :term:`Response` interface, documented within :ref:`the_response`. View configuration can vary the renderer associated with a view callable via the ``renderer`` attribute. For example, this call to :meth:`pyramid.configuration.Configurator.add_view` associates the ``json`` renderer with a view callable: .. code-block:: python :linenos: config.add_view('myproject.views.my_view', renderer='json') When this configuration is added to an application, the ``myproject.views.my_view`` view callable will now use a ``json`` renderer, which renders view return values to a :term:`JSON` serialization. Other built-in renderers include renderers which use the :term:`Chameleon` templating language to render a dictionary to a response. If the :term:`view callable` associated with a :term:`view configuration` returns a Response object directly (an object with the attributes ``status``, ``headerlist`` and ``app_iter``), any renderer associated with the view configuration is ignored, and the response is passed back to :mod:`pyramid` unmolested. For example, if your view callable returns an instance of the :class:`pyramid.httpexceptions.HTTPFound` class as a response, no renderer will be employed. .. code-block:: python :linenos: from pyramid.httpexceptions import HTTPFound def view(request): return HTTPFound(location='http://example.com') # renderer avoided Views which use a renderer can vary non-body response attributes (such as headers and the HTTP status code) by attaching properties to the request. See :ref:`response_request_attrs`. Additional renderers can be added to the system as necessary (see :ref:`adding_and_overriding_renderers`). .. index:: single: renderers (built-in) single: built-in renderers .. _built_in_renderers: Built-In Renderers ~~~~~~~~~~~~~~~~~~ Several built-in "renderers" exist in :mod:`pyramid`. These renderers can be used in the ``renderer`` attribute of view configurations. .. index:: pair: renderer; string ``string``: String Renderer +++++++++++++++++++++++++++ The ``string`` renderer is a renderer which renders a view callable result to a string. If a view callable returns a non-Response object, and the ``string`` renderer is associated in that view's configuration, the result will be to run the object through the Python ``str`` function to generate a string. Note that if a Unicode object is returned by the view callable, it is not ``str()`` -ified. Here's an example of a view that returns a dictionary. If the ``string`` renderer is specified in the configuration for this view, the view will render the returned dictionary to the ``str()`` representation of the dictionary: .. code-block:: python :linenos: from pyramid.response import Response from pyramid.view import view_config @view_config(renderer='string') def hello_world(request): return {'content':'Hello!'} The body of the response returned by such a view will be a string representing the ``str()`` serialization of the return value: .. code-block:: python :linenos: {'content': 'Hello!'} Views which use the string renderer can vary non-body response attributes by attaching properties to the request. See :ref:`response_request_attrs`. .. index:: pair: renderer; JSON ``json``: JSON Renderer +++++++++++++++++++++++ The ``json`` renderer is a renderer which renders view callable results to :term:`JSON`. If a view callable returns a non-Response object it is called. It passes the return value through the ``json.dumps`` standard library function, and wraps the result in a response object. It also sets the response content-type to ``application/json``. Here's an example of a view that returns a dictionary. If the ``json`` renderer is specified in the configuration for this view, the view will render the returned dictionary to a JSON serialization: .. code-block:: python :linenos: from pyramid.response import Response from pyramid.view import view_config @view_config(renderer='json') def hello_world(request): return {'content':'Hello!'} The body of the response returned by such a view will be a string representing the JSON serialization of the return value: .. code-block:: python :linenos: '{"content": "Hello!"}' The return value needn't be a dictionary, but the return value must contain values serializable by :func:`json.dumps`. You can configure a view to use the JSON renderer by naming ``json`` as the ``renderer`` argument of a view configuration, e.g. by using :meth:`pyramid.configuration.Configurator.add_view`: .. code-block:: python :linenos: config.add_view('myproject.views.hello_world', name='hello' context='myproject.models.Hello', renderer='json') Views which use the JSON renderer can vary non-body response attributes by attaching properties to the request. See :ref:`response_request_attrs`. .. index:: pair: renderer; chameleon .. _chameleon_template_renderers: ``*.pt`` or ``*.txt``: Chameleon Template Renderers +++++++++++++++++++++++++++++++++++++++++++++++++++ Two built-in renderers exist for :term:`Chameleon` templates. If the ``renderer`` attribute of a view configuration is an absolute path, a relative path or :term:`resource specification` which has a final path element with a filename extension of ``.pt``, the Chameleon ZPT renderer is used. See :ref:`chameleon_zpt_templates` for more information about ZPT templates. If the ``renderer`` attribute of a view configuration is an absolute path or a :term:`resource specification` which has a final path element with a filename extension of ``.txt``, the :term:`Chameleon` text renderer is used. See :ref:`chameleon_zpt_templates` for more information about Chameleon text templates. The behavior of these renderers is the same, except for the engine used to render the template. When a ``renderer`` attribute that names a template path or :term:`resource specification` (e.g. ``myproject:templates/foo.pt`` or ``myproject:templates/foo.txt``) is used, the view must return a :term:`Response` object or a Python *dictionary*. If the view callable with an associated template returns a Python dictionary, the named template will be passed the dictionary as its keyword arguments, and the template renderer implementation will return the resulting rendered template in a response to the user. If the view callable returns anything but a Response object or a dictionary, an error will be raised. Before passing keywords to the template, the keyword arguments derived from the dictionary returned by the view are augmented. The callable object -- whatever object was used to define the ``view`` -- will be automatically inserted into the set of keyword arguments passed to the template as the ``view`` keyword. If the view callable was a class, the ``view`` keyword will be an instance of that class. Also inserted into the keywords passed to the template are ``renderer_name`` (the string used in the ``renderer`` attribute of the directive), ``renderer_info`` (an object containing renderer-related information), ``context`` (the context of the view used to render the template), and ``request`` (the request passed to the view used to render the template). Here's an example view configuration which uses a Chameleon ZPT renderer: .. code-block:: python :linenos: # config is an instance of pyramid.configuration.Configurator config.add_view('myproject.views.hello_world', name='hello', context='myproject.models.Hello', renderer='myproject:templates/foo.pt') Here's an example view configuration which uses a Chameleon text renderer: .. code-block:: python :linenos: config.add_view('myproject.views.hello_world', name='hello', context='myproject.models.Hello', renderer='myproject:templates/foo.txt') Views which use a Chameleon renderer can vary response attributes by attaching properties to the request. See :ref:`response_request_attrs`. .. index:: pair: renderer; mako .. _mako_template_renderers: ``*.mak`` or ``*.mako``: Mako Template Renderer +++++++++++++++++++++++++++++++++++++++++++++++ The ``Mako`` template renderer is a renderer which renders a Mako template. When used, the view must return a Response object or a Python *dictionary*. The dictionary items will then be used in the global template space. If the view callable returns anything but a Response object or a dictionary, an error will be raised. When using the ``renderer`` attribute to specify a Mako template, the template can be specified in two ways. First, a relative path can be used to name a Mako template relative to the configured Mako template directories. Second, a :term:`resource specification` can be used to locate a template to render. These two styles of naming a template to render also carry through to Mako templates, so that Mako template's can inherit using a :term:`resource specification` if desired. Here's an example view configuration which uses a relative path: .. code-block:: python :linenos: # config is an instance of pyramid.configuration.Configurator config.add_view('myproject.views.hello_world', name='hello', context='myproject.models.Hello', renderer='foo.mak') It's important to note that in Mako's case, the 'relative' path name ``foo.mak`` above is not relative to the package, but is relative to the directory (or directories) configured for Mako via the ``mako.directories`` configuration file setting. The renderer can also be provided in :term:`resource specification` format. Here's an example view configuration which uses a :term:`resource specification`: .. code-block:: python :linenos: config.add_view('myproject.views.hello_world', name='hello', context='myproject.models.Hello', renderer='mypackage:templates/foo.mak') The above configuration will use the file named ``foo.mak`` in the ``templates`` directory of the ``mypackage`` package. The ``Mako`` template renderer can take additional arguments beyond the standard ``reload_templates`` setting, see the :ref:`environment_chapter` for additional :ref:`mako_template_renderer_settings`. .. index:: single: response headers (from a renderer) single: renderer response headers .. _response_request_attrs: Varying Attributes of Rendered Responses ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Before a response that is constructed as the result of the use of a :term:`renderer` is returned to :mod:`pyramid`, several attributes of the request are examined which have the potential to influence response behavior. View callables that don't directly return a response should set these values on the ``request`` object via ``setattr`` within the view callable to influence associated response attributes. ``response_content_type`` Defines the content-type of the resulting response, e.g. ``text/xml``. ``response_headerlist`` A sequence of tuples describing cookie values that should be set in the response, e.g. ``[('Set-Cookie', 'abc=123'), ('X-My-Header', 'foo')]``. ``response_status`` A WSGI-style status code (e.g. ``200 OK``) describing the status of the response. ``response_charset`` The character set (e.g. ``UTF-8``) of the response. ``response_cache_for`` A value in seconds which will influence ``Cache-Control`` and ``Expires`` headers in the returned response. The same can also be achieved by returning various values in the ``response_headerlist``, this is purely a convenience. For example, if you need to change the response status from within a view callable that uses a renderer, assign the ``response_status`` attribute to the request before returning a result: .. code-block:: python :linenos: from pyramid.view import view_config @view_config(name='gone', renderer='templates/gone.pt') def myview(request): request.response_status = '404 Not Found' return {'URL':request.URL} For more information on attributes of the request, see the API documentation in :ref:`request_module`. .. index:: single: renderer (adding) .. _adding_and_overriding_renderers: Adding and Overriding Renderers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ New templating systems and serializers can be associated with :mod:`pyramid` renderer names. To this end, configuration declarations can be made which override an existing :term:`renderer factory` and which add a new renderer factory. Renderers can be registered imperatively using the :meth:`pyramid.configuration.Configurator.add_renderer` API. .. note:: The tasks described in this section can also be performed via :term:`declarative configuration`. See :ref:`zcml_adding_and_overriding_renderers`. For example, to add a renderer which renders views which have a ``renderer`` attribute that is a path that ends in ``.jinja2``: .. code-block:: python :linenos: config.add_renderer('.jinja2', 'mypackage.MyJinja2Renderer') The first argument is the renderer name. The second argument is a reference to an implementation of a :term:`renderer factory` or a :term:`dotted Python name` referring to such an object. .. _adding_a_renderer: Adding a New Renderer +++++++++++++++++++++ You may a new renderer by creating and registering a :term:`renderer factory`. A renderer factory implementation is usually a class which has the following interface: .. code-block:: python :linenos: class RendererFactory: def __init__(self, info): """ Constructor: ``info`` will be an object having the the following attributes: ``name`` (the renderer name), ``package`` (the package that was 'current' at the time the renderer was registered), ``type`` (the renderer type name), ``registry`` (the current application registry) and ``settings`` (the deployment settings dictionary). """ def __call__(self, value, system): """ Call a the renderer implementation with the value and the system value passed in as arguments and return the result (a string or unicode object). The value is the return value of a view. The system value is a dictionary containing available system values (e.g. ``view``, ``context``, and ``request``). """ The formal interface definition of the ``info`` object passed to a renderer factory constructor is available as :class:`pyramid.interfaces.IRendererInfo`. There are essentially two different kinds of renderer factories: - A renderer factory which expects to accept a :term:`resource specification` or an absolute path as the ``name`` attribute of the ``info`` object fed to its constructor. These renderer factories are registered with a ``name`` value that begins with a dot (``.``). These types of renderer factories usually relate to a file on the filesystem, such as a template. - A renderer factory which expects to accept a token that does not represent a filesystem path or a resource specification in the ``name`` attribute of the ``info`` object fed to its constructor. These renderer factories are registered with a ``name`` value that does not begin with a dot. These renderer factories are typically object serializers. .. sidebar:: Resource Specifications A resource specification is a colon-delimited identifier for a :term:`resource`. The colon separates a Python :term:`package` name from a package subpath. For example, the resource specification ``my.package:static/baz.css`` identifies the file named ``baz.css`` in the ``static`` subdirectory of the ``my.package`` Python :term:`package`. Here's an example of the registration of a simple renderer factory via :meth:`pyramid.configuration.Configurator.add_renderer`: .. code-block:: python :linenos: # config is an instance of pyramid.configuration.Configurator config.add_renderer(name='amf', factory='my.package.MyAMFRenderer') Adding the above code to your application startup configuration will allow you to use the ``my.package.MyAMFRenderer`` renderer factory implementation in view configurations by referring to it as ``amf`` in the ``renderer`` attribute of a :term:`view configuration`: .. code-block:: python :linenos: from pyramid.view import view_config @view_config(renderer='amf') def myview(request): return {'Hello':'world'} At startup time, when a :term:`view configuration` is encountered which has a ``name`` argument that does not contain a dot, such as the above ``amf`` is encountered, the full value of the ``name`` attribute is used to construct a renderer from the associated renderer factory. In this case, the view configuration will create an instance of an ``AMFRenderer`` for each view configuration which includes ``amf`` as its renderer value. The ``name`` passed to the ``AMFRenderer`` constructor will always be ``amf``. Here's an example of the registration of a more complicated renderer factory, which expects to be passed a filesystem path: .. code-block:: python :linenos: config.add_renderer(name='.jinja2', factory='my.package.MyJinja2Renderer') Adding the above code to your application startup will allow you to use the ``my.package.MyJinja2Renderer`` renderer factory implementation in view configurations by referring to any ``renderer`` which *ends in* ``.jinja`` in the ``renderer`` attribute of a :term:`view configuration`: .. code-block:: python :linenos: from pyramid.view import view_config @view_config(renderer='templates/mytemplate.jinja2') def myview(request): return {'Hello':'world'} When a :term:`view configuration` which has a ``name`` attribute that does contain a dot, such as ``templates/mytemplate.jinja2`` above is encountered at startup time, the value of the name attribute is split on its final dot. The second element of the split is typically the filename extension. This extension is used to look up a renderer factory for the configured view. Then the value of ``renderer`` is passed to the factory to create a renderer for the view. In this case, the view configuration will create an instance of a ``Jinja2Renderer`` for each view configuration which includes anything ending with ``.jinja2`` as its ``renderer`` value. The ``name`` passed to the ``Jinja2Renderer`` constructor will be whatever the user passed as ``renderer=`` to the view configuration. See also :ref:`renderer_directive` and :meth:`pyramid.configuration.Configurator.add_renderer`. Overriding an Existing Renderer +++++++++++++++++++++++++++++++ You can associate more than one filename extension with the same existing renderer implementation as necessary if you need to use a different file extension for the same kinds of templates. For example, to associate the ``.zpt`` extension with the Chameleon ZPT renderer factory, use the :meth:`pyramid.configuration.Configurator.add_renderer` method: .. code-block:: python :linenos: config.add_renderer('.zpt', 'pyramid.chameleon_zpt.renderer_factory') After you do this, :mod:`pyramid` will treat templates ending in both the ``.pt`` and ``.zpt`` filename extensions as Chameleon ZPT templates. To override the default mapping in which files with a ``.pt`` extension are rendered via a Chameleon ZPT page template renderer, use a variation on the following in your application's startup code: .. code-block:: python :linenos: config.add_renderer('.pt', 'mypackage.pt_renderer') After you do this, the :term:`renderer factory` in ``mypackage.pt_renderer`` will be used to render templates which end in ``.pt``, replacing the default Chameleon ZPT renderer. To associate a *default* renderer with *all* view configurations (even ones which do not possess a ``renderer`` attribute), use a variation on the following (ie. pass ``None`` as the ``name`` attribute to the renderer tag): .. code-block:: python :linenos: config.add_renderer(None, 'mypackage.json_renderer_factory') .. index:: single: view exceptions .. _special_exceptions_in_callables: Using Special Exceptions In View Callables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Usually when a Python exception is raised within a view callable, :mod:`pyramid` allows the exception to propagate all the way out to the :term:`WSGI` server which invoked the application. However, for convenience, two special exceptions exist which are always handled by :mod:`pyramid` itself. These are :exc:`pyramid.exceptions.NotFound` and :exc:`pyramid.exceptions.Forbidden`. Both are exception classes which accept a single positional constructor argument: a ``message``. If :exc:`pyramid.exceptions.NotFound` is raised within view code, the result of the :term:`Not Found View` will be returned to the user agent which performed the request. If :exc:`pyramid.exceptions.Forbidden` is raised within view code, the result of the :term:`Forbidden View` will be returned to the user agent which performed the request. In all cases, the message provided to the exception constructor is made available to the view which :mod:`pyramid` invokes as ``request.exception.args[0]``. .. index:: single: exception views .. _exception_views: Exception Views ~~~~~~~~~~~~~~~~ The machinery which allows the special :exc:`pyramid.exceptions.NotFound` and :exc:`pyramid.exceptions.Forbidden` exceptions to be caught by specialized views as described in :ref:`special_exceptions_in_callables` can also be used by application developers to convert arbitrary exceptions to responses. To register a view that should be called whenever a particular exception is raised from with :mod:`pyramid` view code, use the exception class or one of its superclasses as the ``context`` of a view configuration which points at a view callable you'd like to generate a response. For example, given the following exception class in a module named ``helloworld.exceptions``: .. code-block:: python :linenos: class ValidationFailure(Exception): def __init__(self, msg): self.msg = msg You can wire a view callable to be called whenever any of your *other* code raises a ``hellworld.exceptions.ValidationFailure`` exception: .. code-block:: python :linenos: from helloworld.exceptions import ValidationFailure @view_config(context=ValidationFailure) def failed_validation(exc, request): response = Response('Failed validation: %s' % exc.msg) response.status_int = 500 return response Assuming that a :term:`scan` was run to pick up this view registration, this view callable will be invoked whenever a ``helloworld.exceptions.ValidationError`` is raised by your application's view code. The same exception raised by a custom root factory or a custom traverser is also caught and hooked. Other normal view predicates can also be used in combination with an exception view registration: .. code-block:: python :linenos: from pyramid.view import view_config from pyramid.exceptions import NotFound from pyramid.httpexceptions import HTTPNotFound @view_config(context=NotFound, route_name='home') def notfound_view(request): return HTTPNotFound() The above exception view names the ``route_name`` of ``home``, meaning that it will only be called when the route matched has a name of ``home``. You can therefore have more than one exception view for any given exception in the system: the "most specific" one will be called when the set of request circumstances which match the view registration. The only view predicate that cannot be not be used successfully when creating an exception view configuration is ``name``. The name used to look up an exception view is always the empty string. Views registered as exception views which have a name will be ignored. .. note:: Normal (non-exception) views registered against a context which inherits from :exc:`Exception` will work normally. When an exception view configuraton is processed, *two* views are registered. One as a "normal" view, the other as an "exception" view. This means that you can use an exception as ``context`` for a normal view. The feature can be used with any view registration mechanism (``@view_config`` decorator, ZCML, or imperative ``add_view`` styles). .. index:: single: unicode, views, and forms single: forms, views, and unicode single: views, forms, and unicode Handling Form Submissions in View Callables (Unicode and Character Set Issues) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Most web applications need to accept form submissions from web browsers and various other clients. In :mod:`pyramid`, form submission handling logic is always part of a :term:`view`. For a general overview of how to handle form submission data using the :term:`WebOb` API, see :ref:`webob_chapter` and `"Query and POST variables" within the WebOb documentation