pecan.core – Pecan Core

The pecan.core module is the base module for creating and extending Pecan. The core logic for processing HTTP requests and responses lives here.

class pecan.core.Pecan(*args, **kw)

Bases: pecan.core.PecanBase

Pecan application object. Generally created using pecan.make_app, rather than being created manually.

Creates a Pecan application instance, which is a WSGI application.

Parameters:
  • root – A string representing a root controller object (e.g., “myapp.controller.root.RootController”)
  • default_renderer – The default template rendering engine to use. Defaults to mako.
  • template_path – A relative file system path (from the project root) where template files live. Defaults to ‘templates’.
  • hooks – A callable which returns a list of pecan.hooks.PecanHook
  • custom_renderers – Custom renderer objects, as a dictionary keyed by engine name.
  • extra_template_vars – Any variables to inject into the template namespace automatically.
  • force_canonical – A boolean indicating if this project should require canonical URLs.
  • guess_content_type_from_ext – A boolean indicating if this project should use the extension in the URL for guessing the content type to return.
  • use_context_locals – When True, pecan.request and pecan.response will be available as thread-local references.
  • request_cls – Can be used to specify a custom pecan.request object. Defaults to pecan.Request.
  • response_cls – Can be used to specify a custom pecan.response object. Defaults to pecan.Response.
pecan.core.abort(status_code, detail='', headers=None, comment=None, **kw)

Raise an HTTP status code, as specified. Useful for returning status codes like 401 Unauthorized or 403 Forbidden.

Parameters:
  • status_code – The HTTP status code as an integer.
  • detail – The message to send along, as a string.
  • headers – A dictionary of headers to send along with the response.
  • comment – A comment to include in the response.
pecan.core.load_app(config, **kwargs)

Used to load a Pecan application and its environment based on passed configuration.

Parameters:config – Can be a dictionary containing configuration, a string which represents a (relative) configuration filename

returns a pecan.Pecan object

pecan.core.override_template(template, content_type=None)

Call within a controller to override the template that is used in your response.

Parameters:
  • template – a valid path to a template file, just as you would specify in an @expose.
  • content_type – a valid MIME type to use for the response.func_closure
pecan.core.redirect(location=None, internal=False, code=None, headers={}, add_slash=False, request=None)

Perform a redirect, either internal or external. An internal redirect performs the redirect server-side, while the external redirect utilizes an HTTP 302 status code.

Parameters:
  • location – The HTTP location to redirect to.
  • internal – A boolean indicating whether the redirect should be internal.
  • code – The HTTP status code to use for the redirect. Defaults to 302.
  • headers – Any HTTP headers to send with the response, as a dictionary.
  • request – The pecan.Request instance to use.
pecan.core.render(template, namespace, app=None)

Render the specified template using the Pecan rendering framework with the specified template namespace as a dictionary. Useful in a controller where you have no template specified in the @expose.

Parameters:
  • template – The path to your template, as you would specify in @expose.
  • namespace – The namespace to use for rendering the template, as a dictionary.
  • app – The instance of pecan.Pecan to use