pylons.controllers.util – Controller Utility functions

Utility functions and classes available for use by Controllers

Pylons subclasses the WebOb webob.Request and webob.Response classes to provide backwards compatible functions for earlier versions of Pylons as well as add a few helper functions to assist with signed cookies.

For reference use, refer to the Request and Response below.

Functions available:

abort(), forward(), etag_cache(), mimetype() and redirect()

Module Contents

class pylons.controllers.util.Request(environ, charset=None, unicode_errors=None, decode_param_names=None, **kw)

Bases: webob.request.BaseRequest

WebOb Request subclass

The WebOb webob.Request has no charset, or other defaults. This subclass adds defaults, along with several methods for backwards compatibility with paste.wsgiwrappers.WSGIRequest.


Legacy method to return the webob.Request.accept_charset


Extract a signed cookie of name from the request

The cookie is expected to have been created with Response.signed_cookie, and the secret should be the same as the one used to sign it.

Any failure in the signature of the data will result in None being returned.

class pylons.controllers.util.Response(body=None, status=None, headerlist=None, app_iter=None, content_type=None, conditional_response=None, charset=<object object>, **kw)

Bases: webob.response.Response

WebOb Response subclass

The WebOb Response has no default content type, or error defaults. This subclass adds defaults, along with several methods for backwards compatibility with paste.wsgiwrappers.WSGIResponse.


The body of the response, as a bytes. This will read in the entire app_iter if necessary.


Save a signed cookie with secret signature

Saves a signed cookie of the pickled data. All other keyword arguments that WebOb.set_cookie accepts are usable and passed to the WebOb set_cookie method after creating the signed cookie value.

pylons.controllers.util.abort(status_code=None, detail='', headers=None, comment=None)

Aborts the request immediately by returning an HTTP exception

In the event that the status_code is a 300 series error, the detail attribute will be used as the Location header should one not be specified in the headers attribute.


Use the HTTP Entity Tag cache for Browser side caching

If a “If-None-Match” header is found, and equivilant to key, then a 304 HTTP message will be returned with the ETag to tell the browser that it should use its current cache of the page.

Otherwise, the ETag header will be added to the response headers.

Suggested use is within a Controller Action like so:

import pylons

class YourController(BaseController):
    def index(self):
        return render('/splash.mako')


This works because etag_cache will raise an HTTPNotModified exception if the ETag received matches the key provided.


Forward the request to a WSGI application. Returns its response.

return forward(FileApp('filename'))
pylons.controllers.util.redirect(url, code=302)

Raises a redirect exception to the specified URL

Optionally, a code variable may be passed with the status code of the redirect, ie:

redirect(url(controller='home', action='index'), code=303)