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]` = null
provide_automatic_options`t.ClassVar[boolNone]` = null
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] = trueCreate a new instance of this view class for every request by default. If a view subclass sets this to False, the same instance is used for every request.

Constructor

Signature

def View() - > null

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.ResponseReturnValueA valid Flask response object, string, tuple, or other supported return type for a route handler

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.

Parameters

NameTypeDescription
namestrThe internal name of the view, used for URL building and endpoint identification
*class_argst.AnyPositional arguments to be passed to the view class constructor when it is instantiated
**class_kwargst.AnyKeyword arguments to be passed to the view class constructor when it is instantiated

Returns

TypeDescription
ft.RouteCallableA callable function that handles incoming requests by instantiating the class and calling its dispatch_request method