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

12. VIEW CONFIGURATION

against the context resource before the view function is actually called. Here’s an example of specifying a permission in a view configuration using add_view():

1

# config is an instance

of pyramid.config.Configurator

2

 

 

3

config.add_route(’add’,

’/add.html’, factory=’mypackage.Blog’)

4

config.add_view(’myproject.views.add_entry’, route_name=’add’,

5

permission=’add’)

 

 

 

When an authorization policy is enabled, this view will be protected with the add permission. The view will not be called if the user does not possess the add permission relative to the current context. Instead the forbidden view result will be returned to the client as per Protecting Views with Permissions.

12.2.2 NotFound Errors

It’s useful to be able to debug NotFound error responses when they occur unexpectedly due to an application registry misconfiguration. To debug these errors, use the PYRAMID_DEBUG_NOTFOUND environment variable or the pyramid.debug_notfound configuration file setting. Details of why a view was not found will be printed to stderr, and the browser representation of the error will include the same information. See Environment Variables and .ini File Settings for more information about how, and where to set these values.

12.3 Influencing HTTP Caching

latex-note.png

This feature is new in Pyramid 1.1.

When a non-None http_cache argument is passed to a view configuration, Pyramid will set Expires and Cache-Control response headers in the resulting response, causing browsers to cache the response data for some time. See http_cache in Non-Predicate Arguments for the allowable values and what they mean.

150

12.4. DEBUGGING VIEW CONFIGURATION

Sometimes it’s undesirable to have these headers set as the result of returning a response from a view, even though you’d like to decorate the view with a view configuration decorator that has http_cache. Perhaps there’s an alternate branch in your view code that returns a response that should never be cacheable, while the “normal” branch returns something that should always be cacheable. If this is the case, set the prevent_auto attribute of the response.cache_control object to a non-False value. For example, the below view callable is configured with a @view_config decorator that indicates any response from the view should be cached for 3600 seconds. However, the view itself prevents caching from taking place unless there’s a should_cache GET or POST variable:

from pyramid.view import view_config

@view_config(http_cache=3600) def view(request):

response = Response()

if not ’should_cache’ in request.params: response.cache_control.prevent_auto = True

return response

Note that the http_cache machinery will overwrite or add to caching headers you set within the view itself unless you use prevent_auto.

You can also turn of the effect of http_cache entirely for the duration of a Pyramid application lifetime. To do so, set the PYRAMID_PREVENT_HTTP_CACHE environment variable or the pyramid.prevent_http_cache configuration value setting to a true value. For more information, see Preventing HTTP Caching.

Note that setting pyramid.prevent_http_cache will have no effect on caching headers that your application code itself sets. It will only prevent caching headers that would have been set by the Pyramid HTTP caching machinery invoked as the result of the http_cache argument to view configuration.

12.4 Debugging View Configuration

See Displaying Matching Views for a Given URL for information about how to display each of the view callables that might match for a given URL. This can be an effective way to figure out why a particular view callable is being called instead of the one you’d like to be called.

151

12. VIEW CONFIGURATION

152

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