Home / Class/ MethodView Class — flask Architecture

MethodView Class — flask Architecture

Architecture documentation for the MethodView class in views.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  3a099be0_ce36_e4ca_abd6_9bb88317cf79["MethodView"]
  9f340103_2bf6_52ec_ab7a_db970d7ab419["View"]
  3a099be0_ce36_e4ca_abd6_9bb88317cf79 -->|extends| 9f340103_2bf6_52ec_ab7a_db970d7ab419
  25d123dc_627c_928a_300c_360bcb8e17f5["views.py"]
  3a099be0_ce36_e4ca_abd6_9bb88317cf79 -->|defined in| 25d123dc_627c_928a_300c_360bcb8e17f5
  88313dd2_8c3e_50a4_7285_c9eec7695ad8["__init_subclass__()"]
  3a099be0_ce36_e4ca_abd6_9bb88317cf79 -->|method| 88313dd2_8c3e_50a4_7285_c9eec7695ad8
  2fffccf8_7bc5_c4b1_1066_6d5aeba834fa["dispatch_request()"]
  3a099be0_ce36_e4ca_abd6_9bb88317cf79 -->|method| 2fffccf8_7bc5_c4b1_1066_6d5aeba834fa

Relationship Graph

Source Code

src/flask/views.py lines 138–191

class MethodView(View):
    """Dispatches request methods to the corresponding instance methods.
    For example, if you implement a ``get`` method, it will be used to
    handle ``GET`` requests.

    This can be useful for defining a REST API.

    :attr:`methods` is automatically set based on the methods defined on
    the class.

    See :doc:`views` for a detailed guide.

    .. code-block:: python

        class CounterAPI(MethodView):
            def get(self):
                return str(session.get("counter", 0))

            def post(self):
                session["counter"] = session.get("counter", 0) + 1
                return redirect(url_for("counter"))

        app.add_url_rule(
            "/counter", view_func=CounterAPI.as_view("counter")
        )
    """

    def __init_subclass__(cls, **kwargs: t.Any) -> None:
        super().__init_subclass__(**kwargs)

        if "methods" not in cls.__dict__:
            methods = set()

            for base in cls.__bases__:
                if getattr(base, "methods", None):
                    methods.update(base.methods)  # type: ignore[attr-defined]

            for key in http_method_funcs:
                if hasattr(cls, key):
                    methods.add(key.upper())

            if methods:
                cls.methods = methods

    def dispatch_request(self, **kwargs: t.Any) -> ft.ResponseReturnValue:
        meth = getattr(self, request.method.lower(), None)

        # If the request method is HEAD and we don't have a handler for it
        # retry with GET.
        if meth is None and request.method == "HEAD":
            meth = getattr(self, "get", None)

        assert meth is not None, f"Unimplemented method {request.method!r}"
        return current_app.ensure_sync(meth)(**kwargs)  # type: ignore[no-any-return]

Defined In

Extends

Frequently Asked Questions

What is the MethodView class?
MethodView is a class in the flask codebase, defined in src/flask/views.py.
Where is MethodView defined?
MethodView is defined in src/flask/views.py at line 138.
What does MethodView extend?
MethodView extends View.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free