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 specifiedcontext
andrequest
and return a response object. This function will returnNone
if a corresponding view callable cannot be found (when no view configuration matches the combination ofname
/context
/ andrequest
).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), apyramid.httpexceptions.HTTPForbidden
exception will be raised. The exception'sargs
attribute explains why the view access was disallowed.If
secure
isFalse
, 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 specifiedcontext
andrequest
and return an iterable object which represents the body of a response. This function will returnNone
if a corresponding view callable cannot be found (when no view configuration matches the combination ofname
/context
/ andrequest
). Additionally, this function will raise aValueError
if a view function is found and called but the view function's result does not have anapp_iter
attribute.You can usually get the bytestring representation of the return value of this function by calling
b''.join(iterable)
, or just usepyramid.view.render_view()
instead.If
secure
isTrue
, 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 security policy), apyramid.httpexceptions.HTTPForbidden
exception will be raised; itsargs
attribute explains why the view access was disallowed.If
secure
isFalse
, 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 specifiedcontext
andrequest
and unwind the view response'sapp_iter
(see View Callable Responses) into a single bytestring. This function will returnNone
if a corresponding view callable cannot be found (when no view configuration matches the combination ofname
/context
/ andrequest
). Additionally, this function will raise aValueError
if a view function is found and called but the view function's result does not have anapp_iter
attribute. This function will returnNone
if a corresponding view cannot be found.If
secure
isTrue
, 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), apyramid.httpexceptions.HTTPForbidden
exception will be raised; itsargs
attribute explains why the view access was disallowed.If
secure
isFalse
, no permission checking is done.
- class view_config(**settings)[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')
pyramid.view.view_config
supports the following keyword arguments:context
,exception
,permission
,name
,request_type
,route_name
,request_method
,request_param
,containment
,xhr
,accept
,header
,path_info
,custom_predicates
,decorator
,mapper
,http_cache
,require_csrf
,match_param
,physical_path
, andview_options
.The meanings of these arguments are the same as the arguments passed to
pyramid.config.Configurator.add_view()
. If any argument is left out, its default will be the equivalentadd_view
default.Two additional keyword arguments which will be passed to the venusian
attach
function are_depth
and_category
._depth
is provided for people who wish to reuse this class from another decorator. The default value is0
and should be specified relative to theview_config
invocation. It will be passed in to the venusianattach
function as the depth of the callstack when Venusian checks if the decorator is being used in a class or module context. It's not often used, but it can be useful in this circumstance._category
sets the decorator category name. It can be useful in combination with thecategory
argument ofscan
to control which views should be processed.See the
venusian.attach()
function in Venusian for more information about the_depth
and_category
arguments.See also
See also Adding View Configuration Using the @view_config Decorator for details about using
pyramid.view.view_config
.Note
Because of a limitation with
venusian.Scanner.scan
, note thatview_config
will work only for the following conditions.In Python packages that have an
__init__.py
file in their directory.See also
On module top level members.
On Python source (
.py
) files. Compiled Python files (.pyc
,.pyo
) without a corresponding source file are ignored.
See also
See also the Venusian documentation.
- view_defaults(**settings)[source]¶
A class decorator which, when applied to a class, will provide defaults for all view configurations that use the class. This decorator accepts all the arguments accepted by
pyramid.view.view_config()
, and each has the same meaning.See @view_defaults Class Decorator for more information.
- class notfound_view_config(**settings)[source]¶
New in version 1.3.
An analogue of
pyramid.view.view_config
which registers a Not Found View usingpyramid.config.Configurator.add_notfound_view()
.The
notfound_view_config
constructor accepts most of the same arguments as the constructor ofpyramid.view.view_config
. It can be used in the same places, and behaves in largely the same way, except it always registers a not found exception view instead of a 'normal' view.Example:
from pyramid.view import notfound_view_config from pyramid.response import Response @notfound_view_config() def notfound(request): return Response('Not found!', status='404 Not Found')
All arguments except
append_slash
have the same meaning aspyramid.view.view_config()
and each predicate argument restricts the set of circumstances under which this notfound view will be invoked.If
append_slash
isTrue
, when the Not Found View is invoked, and the current path info does not end in a slash, the notfound logic will attempt to find a route that matches the request's path info suffixed with a slash. If such a route exists, Pyramid will issue a redirect to the URL implied by the route; if it does not, Pyramid will return the result of the view callable provided asview
, as normal.If the argument provided as
append_slash
is not a boolean but instead implementsIResponse
, the append_slash logic will behave as ifappend_slash=True
was passed, but the provided class will be used as the response class instead of the defaultHTTPTemporaryRedirect
response class when a redirect is performed. For example:from pyramid.httpexceptions import ( HTTPMovedPermanently, HTTPNotFound ) @notfound_view_config(append_slash=HTTPMovedPermanently) def aview(request): return HTTPNotFound('not found')
The above means that a redirect to a slash-appended route will be attempted, but instead of
HTTPTemporaryRedirect
being used,HTTPMovedPermanently will be used
for the redirect response if a slash-appended route is found.See Changing the Not Found View for detailed usage information.
Changed in version 1.9.1: Added the
_depth
and_category
arguments.
- class forbidden_view_config(**settings)[source]¶
New in version 1.3.
An analogue of
pyramid.view.view_config
which registers a forbidden view usingpyramid.config.Configurator.add_forbidden_view()
.The forbidden_view_config constructor accepts most of the same arguments as the constructor of
pyramid.view.view_config
. It can be used in the same places, and behaves in largely the same way, except it always registers a forbidden exception view instead of a 'normal' view.Example:
from pyramid.view import forbidden_view_config from pyramid.response import Response @forbidden_view_config() def forbidden(request): return Response('You are not allowed', status='403 Forbidden')
All arguments passed to this function have the same meaning as
pyramid.view.view_config()
and each predicate argument restricts the set of circumstances under which this notfound view will be invoked.See Changing the Forbidden View for detailed usage information.
Changed in version 1.9.1: Added the
_depth
and_category
arguments.
- class exception_view_config(*args, **settings)[source]¶
New in version 1.8.
An analogue of
pyramid.view.view_config
which registers an exception view usingpyramid.config.Configurator.add_exception_view()
.The
exception_view_config
constructor requires an exception context, and additionally accepts most of the same arguments as the constructor ofpyramid.view.view_config
. It can be used in the same places, and behaves in largely the same way, except it always registers an exception view instead of a "normal" view that dispatches on the request context.Example:
from pyramid.view import exception_view_config from pyramid.response import Response @exception_view_config(ValueError, renderer='json') def error_view(request): return {'error': str(request.exception)}
All arguments passed to this function have the same meaning as
pyramid.view.view_config()
, and each predicate argument restricts the set of circumstances under which this exception view will be invoked.Changed in version 1.9.1: Added the
_depth
and_category
arguments.