summaryrefslogtreecommitdiff
path: root/docs/narr/views.rst
diff options
context:
space:
mode:
Diffstat (limited to 'docs/narr/views.rst')
-rw-r--r--docs/narr/views.rst190
1 files changed, 95 insertions, 95 deletions
diff --git a/docs/narr/views.rst b/docs/narr/views.rst
index 11d9f778a..a53063f78 100644
--- a/docs/narr/views.rst
+++ b/docs/narr/views.rst
@@ -58,12 +58,12 @@ accepts a single argument named ``request``, and which returns a
implemented as a function:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.response import Response
+ from pyramid.response import Response
- def hello_world(request):
- return Response('Hello world!')
+ def hello_world(request):
+ return Response('Hello world!')
.. index::
single: view calling convention
@@ -90,16 +90,16 @@ parameters. Views defined as classes must have the following traits.
For example:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.response import Response
+ from pyramid.response import Response
- class MyView(object):
- def __init__(self, request):
- self.request = request
+ class MyView(object):
+ def __init__(self, request):
+ self.request = request
- def __call__(self):
- return Response('hello')
+ 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`.
@@ -127,12 +127,12 @@ implements the :term:`Response` interface is to return a
:class:`pyramid.response.Response` object instance directly. For example:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.response import Response
+ from pyramid.response import Response
- def view(request):
- return Response('OK')
+ def view(request):
+ return Response('OK')
:app:`Pyramid` provides a range of different "exception" classes which inherit
from :class:`pyramid.response.Response`. For example, an instance of the class
@@ -186,23 +186,23 @@ be raised. This will cause a response to be generated with a ``401
Unauthorized`` status:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.httpexceptions import HTTPUnauthorized
+ from pyramid.httpexceptions import HTTPUnauthorized
- def aview(request):
- raise HTTPUnauthorized()
+ def aview(request):
+ raise HTTPUnauthorized()
An HTTP exception, instead of being raised, can alternately be *returned* (HTTP
exceptions are also valid response objects):
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.httpexceptions import HTTPUnauthorized
+ from pyramid.httpexceptions import HTTPUnauthorized
- def aview(request):
- return HTTPUnauthorized()
+ def aview(request):
+ return HTTPUnauthorized()
A shortcut for creating an HTTP exception is the
:func:`pyramid.httpexceptions.exception_response` function. This function
@@ -213,12 +213,12 @@ the :func:`~pyramid.httpexceptions.exception_response` function to construct
and return the same object.
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.httpexceptions import exception_response
+ from pyramid.httpexceptions import exception_response
- def aview(request):
- raise exception_response(401)
+ def aview(request):
+ raise exception_response(401)
This is the case because ``401`` is the HTTP status code for "HTTP
Unauthorized". Therefore, ``raise exception_response(401)`` is functionally
@@ -277,26 +277,26 @@ For example, given the following exception class in a module named
``helloworld.exceptions``:
.. code-block:: python
- :linenos:
+ :linenos:
- class ValidationFailure(Exception):
- def __init__(self, msg):
- self.msg = msg
+ 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 ``helloworld.exceptions.ValidationFailure`` exception:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.view import exception_view_config
- from helloworld.exceptions import ValidationFailure
+ from pyramid.view import exception_view_config
+ from helloworld.exceptions import ValidationFailure
- @exception_view_config(ValidationFailure)
- def failed_validation(exc, request):
- response = Response('Failed validation: %s' % exc.msg)
- response.status_int = 500
- return response
+ @exception_view_config(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
@@ -308,16 +308,16 @@ Other normal view predicates can also be used in combination with an exception
view registration:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.view import view_config
- from helloworld.exceptions import ValidationFailure
+ from pyramid.view import view_config
+ from helloworld.exceptions import ValidationFailure
- @exception_view_config(ValidationFailure, route_name='home')
- def failed_validation(exc, request):
- response = Response('Failed validation: %s' % exc.msg)
- response.status_int = 500
- return response
+ @exception_view_config(ValidationFailure, route_name='home')
+ def failed_validation(exc, request):
+ response = Response('Failed validation: %s' % exc.msg)
+ response.status_int = 500
+ return response
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
@@ -374,22 +374,22 @@ instance of this class will cause the client to receive a "302 Found" response.
To do so, you can *return* a :class:`pyramid.httpexceptions.HTTPFound` instance.
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.httpexceptions import HTTPFound
+ from pyramid.httpexceptions import HTTPFound
- def myview(request):
- return HTTPFound(location='http://example.com')
+ def myview(request):
+ return HTTPFound(location='http://example.com')
Alternately, you can *raise* an HTTPFound exception instead of returning one.
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.httpexceptions import HTTPFound
+ from pyramid.httpexceptions import HTTPFound
- def myview(request):
- raise HTTPFound(location='http://example.com')
+ def myview(request):
+ raise HTTPFound(location='http://example.com')
When the instance is raised, it is caught by the default :term:`exception
response` handler and turned into a response.
@@ -439,22 +439,22 @@ As an example, let's assume that the following form page is served up to a
browser client, and its ``action`` points at some :app:`Pyramid` view code:
.. code-block:: xml
- :linenos:
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- </head>
- <form method="POST" action="myview">
- <div>
- <input type="text" name="firstname"/>
- </div>
- <div>
- <input type="text" name="lastname"/>
- </div>
- <input type="submit" value="Submit"/>
- </form>
- </html>
+ :linenos:
+
+ <html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ </head>
+ <form method="POST" action="myview">
+ <div>
+ <input type="text" name="firstname"/>
+ </div>
+ <div>
+ <input type="text" name="lastname"/>
+ </div>
+ <input type="submit" value="Submit"/>
+ </form>
+ </html>
The ``myview`` view code in the :app:`Pyramid` application *must* expect that
the values returned by ``request.params`` will be of type ``unicode``, as
@@ -462,23 +462,23 @@ opposed to type ``str``. The following will work to accept a form post from the
above form:
.. code-block:: python
- :linenos:
+ :linenos:
- def myview(request):
- firstname = request.params['firstname']
- lastname = request.params['lastname']
+ def myview(request):
+ firstname = request.params['firstname']
+ lastname = request.params['lastname']
But the following ``myview`` view code *may not* work, as it tries to decode
already-decoded (``unicode``) values obtained from ``request.params``:
.. code-block:: python
- :linenos:
+ :linenos:
- def myview(request):
- # the .decode('utf-8') will break below if there are any high-order
- # characters in the firstname or lastname
- firstname = request.params['firstname'].decode('utf-8')
- lastname = request.params['lastname'].decode('utf-8')
+ def myview(request):
+ # the .decode('utf-8') will break below if there are any high-order
+ # characters in the firstname or lastname
+ firstname = request.params['firstname'].decode('utf-8')
+ lastname = request.params['lastname'].decode('utf-8')
For implicit decoding to work reliably, you should ensure that every form you
render that posts to a :app:`Pyramid` view explicitly defines a charset
@@ -553,30 +553,30 @@ The following types work as view callables in this style:
and a ``__call__`` method which accepts no arguments, e.g.:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.response import Response
+ from pyramid.response import Response
- class view(object):
- def __init__(self, context, request):
- self.context = context
- self.request = request
+ class view(object):
+ def __init__(self, context, request):
+ self.context = context
+ self.request = request
- def __call__(self):
- return Response('OK')
+ def __call__(self):
+ return Response('OK')
#. Arbitrary callables that have a ``__call__`` method that accepts ``context,
request``, e.g.:
.. code-block:: python
- :linenos:
+ :linenos:
- from pyramid.response import Response
+ from pyramid.response import Response
- class View(object):
- def __call__(self, context, request):
- return Response('OK')
- view = View() # this is the view callable
+ 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