> 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.httpexceptions.HTTPForbidden 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.httpexceptions.HTTPForbidden 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.httpexceptions.HTTPForbidden 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.

Warning

This function is deprecated as of Pyramid 1.1. New code should not use it. Instead, new code should use the pyramid.request.Request.is_response() method.

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, http_cache=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, mapper, and http_cache.

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

See Adding 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]

Backwards compatibility alias for pyramid.static.static_view; it overrides that class’ constructor to pass use_subpath=True by default. This class is deprecated as of Pyramid 1.1. Use pyramid.static.static_view instead (probably with a use_subpath=True argument).

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. Note also that static routes are not considered when attempting to find a matching route.

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

from pyramid.httpexceptions import HTTPNotFound
from pyramid.view import append_slash_notfound_view
config.add_view(append_slash_notfound_view, context=HTTPNotFound)

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.httpexceptions import HTTPNotFound
from pyramid.view import AppendSlashNotFoundViewFactory

def notfound_view(context, request): return HTTPNotFound('nope')

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

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