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

pyramid.view

render_view_to_response(context, request, name='', secure=True)[source]

Call the view callable configured with a view configuration that matches the view name name registered against the specified context and request and return a response object. This function will return None if a corresponding view callable cannot be found (when no view configuration matches the combination of name / context / and request).

If secure` is True, and the view callable found is protected by a permission, the permission will be checked before calling the view function. If the permission check disallows view execution (based on the current authorization policy), a pyramid.exceptions.Forbidden exception will be raised. The exception’s args attribute explains why the view access was disallowed.

If secure is False, no permission checking is done.

render_view_to_iterable(context, request, name='', secure=True)[source]

Call the view callable configured with a view configuration that matches the view name name registered against the specified context and request and return an iterable object which represents the body of a response. This function will return None if a corresponding view callable cannot be found (when no view configuration matches the combination of name / context / and request). Additionally, this function will raise a ValueError if a view function is found and called but the view function’s result does not have an app_iter attribute.

You can usually get the string representation of the return value of this function by calling ''.join(iterable), or just use pyramid.view.render_view() instead.

If secure is True, and the view is protected by a permission, the permission will be checked before the view function is invoked. If the permission check disallows view execution (based on the current authentication policy), a pyramid.exceptions.Forbidden exception will be raised; its args attribute explains why the view access was disallowed.

If secure is False, no permission checking is done.

render_view(context, request, name='', secure=True)[source]

Call the view callable configured with a view configuration that matches the view name name registered against the specified context and request and unwind the view response’s app_iter (see View Callable Responses) into a single string. This function will return None if a corresponding view callable cannot be found (when no view configuration matches the combination of name / context / and request). Additionally, this function will raise a ValueError if a view function is found and called but the view function’s result does not have an app_iter attribute. This function will return None if a corresponding view cannot be found.

If secure is True, and the view is protected by a permission, the permission will be checked before the view is invoked. If the permission check disallows view execution (based on the current authorization policy), a pyramid.exceptions.Forbidden exception will be raised; its args attribute explains why the view access was disallowed.

If secure is False, no permission checking is done.

is_response(ob)[source]

Return True if ob implements the interface implied by View Callable Responses. False if not.

Note

This isn’t a true interface or subclass check. Instead, it’s a duck-typing check, as response objects are not obligated to be of a particular class or provide any particular Zope interface.

class view_config(name='', request_type=None, for_=None, permission=None, route_name=None, request_method=None, request_param=None, containment=None, attr=None, renderer=None, wrapper=None, xhr=False, accept=None, header=None, path_info=None, custom_predicates=(), context=None, decorator=None, mapper=None)[source]

A function, class or method decorator which allows a developer to create view registrations nearer to a view callable definition than use imperative configuration to do the same.

For example, this code in a module views.py:

from resources import MyResource

@view_config(name='my_view', context=MyResource, permission='read',
             route_name='site1')
def my_view(context, request):
    return 'OK'

Might replace the following call to the pyramid.config.Configurator.add_view() method:

import views
from resources import MyResource
config.add_view(views.my_view, context=MyResource, name='my_view',
                permission='read', 'route_name='site1')

The following arguments are supported as arguments to pyramid.view.view_config: context, permission, name, request_type, route_name, request_method, request_param, containment, xhr, accept, header, path_info, custom_predicates, decorator, and mapper.

The meanings of these arguments are the same as the arguments passed to pyramid.config.Configurator.add_view().

See View Configuration Using the @view_config Decorator for details about using view_config.

class static(root_dir, cache_max_age=3600, package_name=None)[source]

An instance of this class is a callable which can act as a Pyramid view callable; this view will serve static files from a directory on disk based on the root_dir you provide to its constructor.

The directory may contain subdirectories (recursively); the static view implementation will descend into these directories as necessary based on the components of the URL in order to resolve a path into a response.

You may pass an absolute or relative filesystem path or a asset specification representing the directory containing static files as the root_dir argument to this class’ constructor.

If the root_dir path is relative, and the package_name argument is None, root_dir will be considered relative to the directory in which the Python file which calls static resides. If the package_name name argument is provided, and a relative root_dir is provided, the root_dir will be considered relative to the Python package specified by package_name (a dotted path to a Python package).

cache_max_age influences the Expires and Max-Age response headers returned by the view (default is 3600 seconds or five minutes).

Note

If the root_dir is relative to a package, or is a asset specification the Pyramid pyramid.config.Configurator method can be used to override assets within the named root_dir package-relative directory. However, if the root_dir is absolute, configuration will not be able to override the assets it contains.

append_slash_notfound_view(context, request)

For behavior like Django’s APPEND_SLASH=True, use this view as the Not Found view in your application.

When this view is the Not Found view (indicating that no view was found), and any routes have been defined in the configuration of your application, if the value of the PATH_INFO WSGI environment variable does not already end in a slash, and if the value of PATH_INFO plus a slash matches any route’s path, do an HTTP redirect to the slash-appended PATH_INFO. Note that this will lose POST data information (turning it into a GET), so you shouldn’t rely on this to redirect POST requests.

Use the pyramid.config.Configurator.add_view() method to configure this view as the Not Found view:

from pyramid.exceptions import NotFound
from pyramid.view import append_slash_notfound_view
config.add_view(append_slash_notfound_view, context=NotFound)

See also Changing the Not Found View.

class AppendSlashNotFoundViewFactory(notfound_view=None)[source]

There can only be one Not Found view in any Pyramid application. Even if you use pyramid.view.append_slash_notfound_view() as the Not Found view, Pyramid still must generate a 404 Not Found response when it cannot redirect to a slash-appended URL; this not found response will be visible to site users.

If you don’t care what this 404 response looks like, and you only need redirections to slash-appended route URLs, you may use the pyramid.view.append_slash_notfound_view() object as the Not Found view. However, if you wish to use a custom notfound view callable when a URL cannot be redirected to a slash-appended URL, you may wish to use an instance of this class as the Not Found view, supplying a view callable to be used as the custom notfound view as the first argument to its constructor. For instance:

from pyramid.exceptions import NotFound
from pyramid.view import AppendSlashNotFoundViewFactory

def notfound_view(context, request):
    return HTTPNotFound('It aint there, stop trying!')

custom_append_slash = AppendSlashNotFoundViewFactory(notfound_view)
config.add_view(custom_append_slash, context=NotFound)

The notfound_view supplied must adhere to the two-argument view callable calling convention of (context, request) (context will be the exception object).

Previous topic

pyramid.url

Next topic

pyramid.wsgi

This Page