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

10. RENDERERS

The above configuration will use the file named foo.mak in the templates directory of the mypackage package.

The Mako template renderer can take additional arguments beyond the standard pyramid.reload_templates setting, see the Environment Variables and .ini File Settings for additional Mako Template Render Settings.

10.4 Varying Attributes of Rendered Responses

Before a response constructed by a renderer is returned to Pyramid, several attributes of the request are examined which have the potential to influence response behavior.

View callables that don’t directly return a response should use the API of the pyramid.response.Response attribute available as request.response during their execution, to influence associated response behavior.

For example, if you need to change the response status from within a view callable that uses a renderer, assign the status attribute to the response attribute of the request before returning a result:

1

2

3

4

5

6

from pyramid.view import view_config

@view_config(name=’gone’, renderer=’templates/gone.pt’) def myview(request):

request.response.status = ’404 Not Found’ return {’URL’:request.URL}

Note that mutations of request.response in views which return a Response object directly will have no effect unless the response object returned is request.response. For example, the following example calls request.response.set_cookie, but this call will have no effect, because a different Response object is returned.

1 from pyramid.response import Response

2

3 def view(request):

4request.response.set_cookie(’abc’, ’123’) # this has no effect

5return Response(’OK’) # because we’re returning a different response

If you mutate request.response and you’d like the mutations to have an effect, you must return request.response:

114

10.5. DEPRECATED MECHANISM TO VARY ATTRIBUTES OF RENDERED RESPONSES

1

2

3

def view(request): request.response.set_cookie(’abc’, ’123’) return request.response

For more information on attributes of the

request,

see

the API documentation

in

pyramid.request.

For more information on

the API

of

request.response,

see

pyramid.request.Request.response.

 

 

 

 

10.5Deprecated Mechanism to Vary Attributes of Rendered Responses

latex-warning.png

This section describes behavior deprecated in Pyramid 1.1.

In previous releases of Pyramid (1.0 and before), the request.response attribute did not exist. Instead, Pyramid required users to set special response_ -prefixed attributes of the request to influence response behavior. As of Pyramid 1.1, those request attributes are deprecated and their use will cause a deprecation warning to be issued when used. Until their existence is removed completely, we document them below, for benefit of people with older code bases.

response_content_type Defines the content-type of the resulting response, e.g. text/xml.

response_headerlist A sequence of tuples describing header values that should be set in the response, e.g. [(’Set-Cookie’, ’abc=123’), (’X-My-Header’, ’foo’)].

response_status A WSGI-style status code (e.g. 200 OK) describing the status of the response.

response_charset The character set (e.g. UTF-8) of the response.

response_cache_for A value in seconds which will influence Cache-Control and Expires headers in the returned response. The same can also be achieved by returning various values in the response_headerlist, this is purely a convenience.

115

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