View
Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking.
Attributes
| Attribute | Type | Description |
|---|---|---|
| http_method_names | list[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
| Name | Type | Description |
|---|---|---|
| **kwargs | dict | Arbitrary 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
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| **initkwargs | dict | Keyword arguments used to initialize class attributes; must correspond to existing class attributes and cannot be HTTP method names. |
Returns
| Type | Description |
|---|---|
function | A 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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current HTTP request object to be stored on the instance. |
| *args | tuple | Positional arguments captured from the URL pattern. |
| **kwargs | dict | Keyword arguments captured from the URL pattern. |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming HTTP request to be dispatched. |
| *args | tuple | Positional arguments to pass to the handler. |
| **kwargs | dict | Keyword arguments to pass to the handler. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The request that used an unsupported HTTP method. |
| *args | tuple | Additional positional arguments. |
| **kwargs | dict | Additional keyword arguments. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming OPTIONS request. |
| *args | tuple | Additional positional arguments. |
| **kwargs | dict | Additional keyword arguments. |
Returns
| Type | Description |
|---|---|
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | An empty HTTP response with the 'Allow' header set to the supported HTTP methods. |