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

55. PYRAMID.RESPONSE

55.1 Functions

response_adapter(*types_or_ifaces)

Decorator activated via a scan which treats the function being decorated as a response adapter for the set of types or interfaces passed as *types_or_ifaces to the decorator constructor.

For example, if you scan the following response adapter:

from pyramid.response import Response

from pyramid.response import response_adapter

@response_adapter(int) def myadapter(i):

return Response(status=i)

You can then return an integer from your view callables, and it will be converted into a response with the integer as the status code.

More than one type or interface can be passed as a constructor argument. The decorated response adapter will be called for each type or interface.

import json

from pyramid.response import Response

from pyramid.response import response_adapter

@response_adapter(dict, list) def myadapter(ob):

return Response(json.dumps(ob))

This method will have no effect until a scan is performed agains the package or module which contains it, ala:

from pyramid.config import Configurator config = Configurator()

config.scan(’somepackage_containing_adapters’)

648

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