> Read the latest version of this page
Edit me on GitHub

pyramid.events

Functions

subscriber(*ifaces)[source]

Decorator activated via a scan which treats the function being decorated as an event subscriber for the set of interfaces passed as *ifaces to the decorator constructor.

For example:

from pyramid.events import NewRequest
from pyramid.events import subscriber

@subscriber(NewRequest)
def mysubscriber(event):
    event.request.foo = 1

More than one event type can be passed as a construtor argument. The decorated subscriber will be called for each event type.

from pyramid.events import NewRequest, NewResponse
from pyramid.events import subscriber

@subscriber(NewRequest, NewResponse)
def mysubscriber(event):
    print event

When the subscriber decorator is used without passing an arguments, the function it decorates is called for every event sent:

from pyramid.events import subscriber

@subscriber()
def mysubscriber(event):
    print event

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

from pyramid.config import Configurator
config = Configurator()
config.scan('somepackage_containing_subscribers')

Event Types

class ApplicationCreated(app)[source]

An instance of this class is emitted as an event when the pyramid.config.Configurator.make_wsgi_app() is called. The instance has an attribute, app, which is an instance of the router that will handle WSGI requests. This class implements the pyramid.interfaces.IApplicationCreated interface.

Note

For backwards compatibility purposes, this class can also be imported as pyramid.events.WSGIApplicationCreatedEvent. This was the name of the event class before Pyramid 1.0.

class NewRequest(request)[source]

An instance of this class is emitted as an event whenever Pyramid begins to process a new request. The even instance has an attribute, request, which is a request object. This event class implements the pyramid.interfaces.INewRequest interface.

class ContextFound(request)[source]

An instance of this class is emitted as an event after the Pyramid router finds a context object (after it performs traversal) but before any view code is executed. The instance has an attribute, request, which is the request object generated by Pyramid.

Notably, the request object will have an attribute named context, which is the context that will be provided to the view which will eventually be called, as well as other attributes attached by context-finding code.

This class implements the pyramid.interfaces.IContextFound interface.

Note

As of Pyramid 1.0, for backwards compatibility purposes, this event may also be imported as pyramid.events.AfterTraversal.

class NewResponse(request, response)[source]

An instance of this class is emitted as an event whenever any Pyramid view or exception view returns a response.

The instance has two attributes:request, which is the request which caused the response, and response, which is the response object returned by a view or renderer.

If the response was generated by an exception view, the request will have an attribute named exception, which is the exception object which caused the exception view to be executed. If the response was generated by a ‘normal’ view, the request will not have this attribute.

This event will not be generated if a response cannot be created due to an exception that is not caught by an exception view (no response is created under this circumstace).

This class implements the pyramid.interfaces.INewResponse interface.

Note

Postprocessing a response is usually better handled in a WSGI middleware component than in subscriber code that is called by a pyramid.interfaces.INewResponse event. The pyramid.interfaces.INewResponse event exists almost purely for symmetry with the pyramid.interfaces.INewRequest event.

class BeforeRender(system)[source]
get(k, default=None)[source]

Return the value for key k from the renderer globals dictionary, or the default if no such value exists.

setdefault(name, default=None)[source]

Return the existing value for name in the renderers globals dictionary. If no value with name exists in the dictionary, set the default value into the renderer globals dictionary under the name passed. If a value already existed in the dictionary, return it. If a value did not exist in the dictionary, return the default

update(d)[source]

Update the renderer globals dictionary with another dictionary d.

See Using Events for more information about how to register code which subscribes to these events.

Table Of Contents

Previous topic

pyramid.config

Next topic

pyramid.exceptions