rest_framework.views

Provides an APIView class that is the base of all views in REST framework.

Module Contents

get_view_name(view)

Given a view instance, return a textual name to represent the view. This name is used in the browsable API, and in OPTIONS responses.

This function is the default for the VIEW_NAME_FUNCTION setting.

get_view_description(view, html=False)

Given a view instance, return a textual description to represent the view. This name is used in the browsable API, and in OPTIONS responses.

This function is the default for the VIEW_DESCRIPTION_FUNCTION setting.

set_rollback()
exception_handler(exc, context)

Returns the response that should be used for any given exception.

By default we handle the REST framework APIException, and also Django’s built-in Http404 and PermissionDenied exceptions.

Any unhandled exceptions may return None, which will cause a 500 error to be raised.

class APIView

Bases:django.views.generic.View

renderer_classes
parser_classes
authentication_classes
throttle_classes
permission_classes
content_negotiation_class
metadata_class
versioning_class
settings
schema
allowed_methods

Wrap Django’s private _allowed_methods interface in a public property.

default_response_headers
classmethod as_view(cls, **initkwargs)

Store the original class on the view function.

This allows us to discover information about the view when we do URL reverse lookups. Used for breadcrumb generation.

http_method_not_allowed(self, request, *args, **kwargs)

If request.method does not correspond to a handler method, determine what kind of exception to raise.

permission_denied(self, request, message=None)

If request is not permitted, determine what kind of exception to raise.

throttled(self, request, wait)

If request is throttled, determine what kind of exception to raise.

get_authenticate_header(self, request)

If a request is unauthenticated, determine the WWW-Authenticate header to use for 401 responses, if any.

get_parser_context(self, http_request)

Returns a dict that is passed through to Parser.parse(), as the parser_context keyword argument.

get_renderer_context(self)

Returns a dict that is passed through to Renderer.render(), as the renderer_context keyword argument.

get_exception_handler_context(self)

Returns a dict that is passed through to EXCEPTION_HANDLER, as the context argument.

get_view_name(self)

Return the view name, as used in OPTIONS responses and in the browsable API.

get_view_description(self, html=False)

Return some descriptive text for the view, as used in OPTIONS responses and in the browsable API.

get_format_suffix(self, **kwargs)

Determine if the request includes a ‘.json’ style format suffix

get_renderers(self)

Instantiates and returns the list of renderers that this view can use.

get_parsers(self)

Instantiates and returns the list of parsers that this view can use.

get_authenticators(self)

Instantiates and returns the list of authenticators that this view can use.

get_permissions(self)

Instantiates and returns the list of permissions that this view requires.

get_throttles(self)

Instantiates and returns the list of throttles that this view uses.

get_content_negotiator(self)

Instantiate and return the content negotiation class to use.

get_exception_handler(self)

Returns the exception handler that this view uses.

perform_content_negotiation(self, request, force=False)

Determine which renderer and media type to use render the response.

perform_authentication(self, request)

Perform authentication on the incoming request.

Note that if you override this and simply ‘pass’, then authentication will instead be performed lazily, the first time either request.user or request.auth is accessed.

check_permissions(self, request)

Check if the request should be permitted. Raises an appropriate exception if the request is not permitted.

check_object_permissions(self, request, obj)

Check if the request should be permitted for a given object. Raises an appropriate exception if the request is not permitted.

check_throttles(self, request)

Check if request should be throttled. Raises an appropriate exception if the request is throttled.

determine_version(self, request, *args, **kwargs)

If versioning is being used, then determine any API version for the incoming request. Returns a two-tuple of (version, versioning_scheme)

initialize_request(self, request, *args, **kwargs)

Returns the initial request object.

initial(self, request, *args, **kwargs)

Runs anything that needs to occur prior to calling the method handler.

finalize_response(self, request, response, *args, **kwargs)

Returns the final response object.

handle_exception(self, exc)

Handle any exception that occurs, by returning an appropriate response, or re-raising the error.

raise_uncaught_exception(self, exc)
dispatch(self, request, *args, **kwargs)

.dispatch() is pretty much the same as Django’s regular dispatch, but with extra hooks for startup, finalize, and exception handling.

options(self, request, *args, **kwargs)

Handler method for HTTP ‘OPTIONS’ request.