class documentation

class View:

Known subclasses: flask.views.MethodView

View In Hierarchy

Alternative way to use view functions. A subclass has to implement dispatch_request which is called with the view arguments from the URL routing system. If methods is provided the methods do not have to be passed to the ~flask.Flask.add_url_rule method explicitly:

class MyView(View):
    methods = ['GET']

    def dispatch_request(self, name):
        return f"Hello {name}!"

app.add_url_rule('/hello/<name>', view_func=MyView.as_view('myview'))

When you want to decorate a pluggable view you will have to either do that when the view function is created (by wrapping the return value of as_view) or you can use the decorators attribute:

class SecretView(View):
    methods = ['GET']
    decorators = [superuser_required]

    def dispatch_request(self):
        ...

The decorators stored in the decorators list are applied one after another when the view function is created. Note that you can not use the class based decorators since those would decorate the view class and not the generated view function!

Class Method as​_view No summary
Method dispatch​_request Subclasses have to override this method to implement the actual view function code. This method is called with all the arguments from the URL rule.
Class Variable decorators Undocumented
Class Variable methods Undocumented
Class Variable provide​_automatic​_options Undocumented
@classmethod
def as_view(cls, name, *class_args, **class_kwargs):

Converts the class into an actual view function that can be used with the routing system. Internally this generates a function on the fly which will instantiate the View on each request and call the dispatch_request method on it.

The arguments passed to as_view are forwarded to the constructor of the class.

Parameters
name:strUndocumented
*class​_args:t.AnyUndocumented
**class​_kwargs:t.AnyUndocumented
Returns
t.CallableUndocumented
def dispatch_request(self):
Subclasses have to override this method to implement the actual view function code. This method is called with all the arguments from the URL rule.
Returns
ResponseReturnValueUndocumented
decorators: t.List[t.Callable] =

Undocumented

methods: t.Optional[t.List[str]] =

Undocumented

provide_automatic_options: t.Optional[bool] =

Undocumented