Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Class-Based View API

Flask Class-Based View API

Flask provides two kinds of class-based views that let you organize view logic in an object-oriented way.

View is the most basic class-based view, while MethodView automatically dispatches by HTTP method.


The View base class

Attribute / method Description
methods Class attribute, the list of HTTP methods the view accepts. Defaults to None (uses the add_url_rule default)
decorators Class attribute, a list of decorators. Applied in order to the generated view function
init_every_request Class attribute, bool. When True (default), a new instance is created for each request; when False, a single instance is reused
provide_automatic_options Class attribute, whether to automatically implement OPTIONS. Defaults to None (uses the application configuration)
dispatch_request(**kwargs) Core method that must be overridden. kwargs are the URL variables
as_view(name, *class_args, **class_kwargs) Class method that creates a view function usable in add_url_rule

The MethodView class

Inherits from View and automatically dispatches to the corresponding instance method by HTTP method.

Method naming rules Description
get(self, **kwargs) Handle GET requests
post(self, **kwargs) Handle POST requests
put(self, **kwargs) Handle PUT requests
delete(self, **kwargs) Handle DELETE requests
patch(self, **kwargs) Handle PATCH requests

Once these methods are defined, the methods attribute is automatically set based on the defined methods.


View vs MethodView

Feature View MethodView
Dispatch methods A single dispatch_request handles all methods Automatic dispatch by method name (get→GET, etc.)
Applicable scenarios General views and template rendering RESTful API

Code Example

Example

from

flask

import

Flask

,

request

,

session

,

redirect

,

url_for

from

flask.

views

import

View

,

MethodView

app

=

Flask

(

name

)

Basic View: a single dispatch_request

class

HelloView

(

View

)

:

   

def

dispatch_request

(

self

,

name

)

:

       

return

f

"

Hello, {name}!

"

app.

add_url_rule

(

"/hello/"

,

view_func

=

HelloView.

as_view

(

"hello"

)

)

MethodView:RESTful API

class

PostAPI

(

MethodView

)

:

   

Custom initialization (called on every request)

   

def

init

(

self

)

:

       

self

.

db

=

get_database_connection

(

)

   

def

get

(

self

,

post_id

=

None

)

:

       

if

post_id

is

None

:

            posts

=

self

.

db

.

query_all_posts

(

)

           

return

{

"posts"

: posts

}

       

return

self

.

db

.

get_post

(

post_id

)

   

def

post

(

self

)

:

        data

=

request.

json

        post_id

=

self

.

db

.

create_post

(

data

[

"title"

]

,

data

[

"body"

]

)

       

return

{

"id"

: post_id

}

,

201

   

def

delete

(

self

,

post_id

)

:

       

self

.

db

.

delete_post

(

post_id

)

       

return

{

"message"

:

"deleted"

}

Registration: the same view function handles multiple methods

post_view

=

PostAPI.

as_view

(

"post_api"

)

app.

add_url_rule

(

"/api/posts"

,

defaults

=

{

"post_id"

:

None

}

,

                 view_func

=

post_view

,

methods

=

[

"GET"

,

"POST"

]

)

app.

add_url_rule

(

"/api/posts/int:post_id"

,

                 view_func

=

post_view

,

methods

=

[

"GET"

,

"DELETE"

]

)