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

9. VIEWS

header. If you return a Content-Type header without an explicit charset, a request will add a ;charset=utf-8 trailer to the Content-Type header value for you, for response content types that are textual (e.g. text/html, application/xml, etc) as it is rendered. If you are using your own response object, you will need to ensure you do this yourself.

latex-note.png

Only the values of request params obtained via request.params, request.GET or request.POST are decoded to Unicode objects implicitly in the Pyramid default configuration. The keys are still (byte) strings.

9.9 Alternate View Callable Argument/Calling Conventions

Usually, view callables are defined to accept only a single argument: request. However, view callables may alternately be defined as classes, functions, or any callable that accept two positional arguments: a context resource as the first argument and a request as the second argument.

The context and request arguments passed to a view function defined in this style can be defined as follows:

context

The resource object found via tree traversal or URL dispatch.

request A Pyramid Request object representing the current WSGI request.

The following types work as view callables in this style:

1. Functions that accept two arguments: context, and request, e.g.:

1 from pyramid.response import Response

2

3 def view(context, request):

4return Response(’OK’)

2.Classes that have an __init__ method that accepts context, request and a __call__ method which accepts no arguments, e.g.:

104

9.10. PYLONS-1.0-STYLE “CONTROLLER” DISPATCH

1

2

3

4

5

6

7

8

9

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’)

3. Arbitrary callables that have a __call__ method that accepts context, request, e.g.:

1 from pyramid.response import Response

2

3 class View(object):

4def __call__(self, context, request):

5

return Response(’OK’)

6

view = View() # this is the view callable

This style of calling convention is most useful for 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.

9.10 Pylons-1.0-Style “Controller” Dispatch

A package named pyramid_handlers (available from PyPI) provides an analogue of Pylons -style “controllers”, which are a special kind of view class which provides more automation when your application uses URL dispatch solely.

105

9. VIEWS

106

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