Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

CHAPTER

NINE

VIEWS

One of the primary jobs of Pyramid is to find and invoke a view callable when a request reaches your application. View callables are bits of code which do something interesting in response to a request made to your application. They are the “meat” of any interesting web application.

latex-note.png

A Pyramid view callable is often referred to in conversational shorthand as a 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.

This chapter describes how view callables should be defined. We’ll have to wait until a following chapter (entitled View Configuration) to find out how we actually tell Pyramid to wire up view callables to particular URL patterns and other request circumstances.

9.1 View Callables

View callables are, at the risk of sounding obvious, callable Python objects. Specifically, view callables can be functions, classes, or instances that implement a __call__ method (making the instance callable).

95

9. VIEWS

View callables must, at a minimum, accept a single argument named request. This argument represents a Pyramid Request object. A request object represents a WSGI environment provided to Pyramid by the upstream WSGI server. As you might expect, the request object contains everything your application needs to know about the specific HTTP request being made.

A view callable’s ultimate responsibility is to create a Pyramid Response object. This can be done by creating a Response object in the view callable code and returning it directly or by raising special kinds of exceptions from within the body of a view callable.

9.2 Defining a View Callable as a Function

One of the easiest way to define a view callable is to create a function that accepts a single argument named request, and which returns a Response object. For example, this is a “hello world” view callable implemented as a function:

1

2

3

4

from pyramid.response import Response

def hello_world(request):

return Response(’Hello world!’)

9.3 Defining a View Callable as a Class

A view callable may also be represented by a Python 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__ method 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 argument.

a __call__ (or other) method that accepts no parameters and which returns a response.

For example:

96

9.4. VIEW CALLABLE RESPONSES

1

from pyramid.response

import Response

2

 

 

3

class MyView(object):

 

4

def __init__(self, request):

5self.request = request

6

7def __call__(self):

8return Response(’hello’)

The request object passed to __init__ is the same type of request object described in Defining a View Callable as a Function.

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 the configuration for the view. See View Configuration Parameters. The same view callable class can be used in different view configuration statements with different attr values, each pointing at a different method of the class if you’d like the class to represent a collection of related view callables.

9.4 View Callable Responses

A view callable may return an object that implements the Pyramid Response interface. The easiest way to return something that implements the Response interface is to return a pyramid.response.Response object instance directly. For example:

1

2

3

4

from pyramid.response import Response

def view(request):

return Response(’OK’)

Pyramid provides a range of different “exception” classes which inherit from pyramid.response.Response. For example, an instance of the class pyramid.httpexceptions.HTTPFound is also a valid response object because it inherits from Response. For examples, see HTTP Exceptions and Using a View Callable to Do an HTTP Redirect.

97

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]