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

10.2. BUILT-IN RENDERERS

You can of course also return the request.response attribute instead to avoid rendering:

1

2

3

4

5

6

from pyramid.view import view_config

@view_config(renderer=’json’) def view(request):

request.response.body = ’OK’

return request.response # json renderer avoided

10.2 Built-In Renderers

Several built-in renderers exist in Pyramid. These renderers can be used in the renderer attribute of view configurations.

10.2.1 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:

1

2

3

4

5

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:

1 {’content’: ’Hello!’}

Views which use the string renderer can vary non-body response attributes by using the API of the request.response attribute. See Varying Attributes of Rendered Responses.

109

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