Skip to main content

View

Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking.

Attributes

AttributeTypeDescription
http_method_nameslist[str] = ["get", "post", "put", "patch", "delete", "head", "options", "trace"]List of lowercase HTTP verb strings that the view is allowed to process during dispatch.

Constructor

Signature

def View(
**kwargs: dict
)

Parameters

NameTypeDescription
**kwargsdictArbitrary keyword arguments to be set as instance attributes.

Methods


view_is_async()

@classmethod
def view_is_async() - > boolean

Determines if the view class is asynchronous by checking its HTTP handler methods. It ensures consistency by requiring that all handlers are either sync or async, raising an error if they are mixed.

Returns

TypeDescription
booleanTrue if the HTTP handlers are coroutine functions, False otherwise.

as_view()

@classmethod
def as_view(
**initkwargs: dict
) - > function

Main entry point for a request-response process.

Parameters

NameTypeDescription
**initkwargsdictKeyword arguments used to initialize class attributes; must correspond to existing class attributes and cannot be HTTP method names.

Returns

TypeDescription
functionA callable view function that takes a request and returns a response, acting as the bridge between the URLconf and the class instance.

setup()

@classmethod
def setup(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
*args: tuple,
**kwargs: dict
) - > null

Initialize attributes shared by all view methods.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current HTTP request object to be stored on the instance.
*argstuplePositional arguments captured from the URL pattern.
**kwargsdictKeyword arguments captured from the URL pattern.

Returns

TypeDescription
null

dispatch()

@classmethod
def dispatch(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
*args: tuple,
**kwargs: dict
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Inspects the HTTP request method and delegates the request to the corresponding class handler (e.g., get(), post()). If the method is not supported or allowed, it routes to the error handler.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request to be dispatched.
*argstuplePositional arguments to pass to the handler.
**kwargsdictKeyword arguments to pass to the handler.

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The HTTP response returned by the specific method handler or the error handler.

http_method_not_allowed()

@classmethod
def http_method_not_allowed(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
*args: tuple,
**kwargs: dict
) - > [HttpResponseNotAllowed](../../../http/response/httpresponsenotallowed.md?sid=django_http_response_httpresponsenotallowed)

Handles requests using HTTP methods that are not supported by the view. It logs the unauthorized attempt and returns a response containing the list of allowed methods.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The request that used an unsupported HTTP method.
*argstupleAdditional positional arguments.
**kwargsdictAdditional keyword arguments.

Returns

TypeDescription
[HttpResponseNotAllowed](../../../http/response/httpresponsenotallowed.md?sid=django_http_response_httpresponsenotallowed)An HTTP 405 response including an 'Allow' header with permitted methods.

options()

@classmethod
def options(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
*args: tuple,
**kwargs: dict
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Handle responding to requests for the OPTIONS HTTP verb.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming OPTIONS request.
*argstupleAdditional positional arguments.
**kwargsdictAdditional keyword arguments.

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)An empty HTTP response with the 'Allow' header set to the supported HTTP methods.