Skip to main content

View

Subclass this class and override :meth:dispatch_request to create a generic class-based view. Call :meth:as_view to create a view function that creates an instance of the class with the given arguments and calls its dispatch_request method with any URL variables.

Attributes

AttributeTypeDescription
methods`t.ClassVar[t.Collection[str]None]` = None
provide_automatic_options`t.ClassVar[boolNone]` = None
decoratorst.ClassVar[list[t.Callable[..., t.Any]]] = []A list of decorators to apply, in order, to the generated view function; remember that @decorator syntax is applied bottom to top, so the first decorator in the list would be the bottom decorator.
init_every_requestt.ClassVar[bool] = TrueControls whether a new instance of this view class is created for every request by default; if a view subclass sets this to False, the same instance is used for every request.

Methods


dispatch_request()

@classmethod
def dispatch_request() - > ft.ResponseReturnValue

The actual view function behavior. Subclasses must override this and return a valid response. Any variables from the URL rule are passed as keyword arguments.

Returns

TypeDescription
ft.ResponseReturnValue

as_view()

@classmethod
def as_view(
name: str,
class_args: t.Any,
class_kwargs: t.Any
) - > ft.RouteCallable

Convert the class into a view function that can be registered for a route. By default, the generated view will create a new instance of the view class for every request and call its :meth:dispatch_request method. If the view class sets :attr:init_every_request to False, the same instance will be used for every request. Except for name, all other arguments passed to this method are forwarded to the view class __init__ method.

Parameters

NameTypeDescription
namestrThe name of the view function to be created.
class_argst.AnyPositional arguments to pass to the view class's constructor.
class_kwargst.AnyKeyword arguments to pass to the view class's constructor.

Returns

TypeDescription
ft.RouteCallable