Home / Function/ as_view() — flask Function Reference

as_view() — flask Function Reference

Architecture documentation for the as_view() function in views.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  eef2cf72_535a_c4db_9a08_d5ed4373f222["as_view()"]
  9f340103_2bf6_52ec_ab7a_db970d7ab419["View"]
  eef2cf72_535a_c4db_9a08_d5ed4373f222 -->|defined in| 9f340103_2bf6_52ec_ab7a_db970d7ab419
  style eef2cf72_535a_c4db_9a08_d5ed4373f222 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/views.py lines 86–135

    def as_view(
        cls, 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.

        .. versionchanged:: 2.2
            Added the ``init_every_request`` class attribute.
        """
        if cls.init_every_request:

            def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
                self = view.view_class(  # type: ignore[attr-defined]
                    *class_args, **class_kwargs
                )
                return current_app.ensure_sync(self.dispatch_request)(**kwargs)  # type: ignore[no-any-return]

        else:
            self = cls(*class_args, **class_kwargs)  # pyright: ignore

            def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
                return current_app.ensure_sync(self.dispatch_request)(**kwargs)  # type: ignore[no-any-return]

        if cls.decorators:
            view.__name__ = name
            view.__module__ = cls.__module__
            for decorator in cls.decorators:
                view = decorator(view)

        # We attach the view class to the view function for two reasons:
        # first of all it allows us to easily figure out what class-based
        # view this thing came from, secondly it's also used for instantiating
        # the view class so you can actually replace it with something else
        # for testing purposes and debugging.
        view.view_class = cls  # type: ignore
        view.__name__ = name
        view.__doc__ = cls.__doc__
        view.__module__ = cls.__module__
        view.methods = cls.methods  # type: ignore
        view.provide_automatic_options = cls.provide_automatic_options  # type: ignore
        return view

Subdomains

Defined In

Frequently Asked Questions

What does as_view() do?
as_view() is a function in the flask codebase, defined in src/flask/views.py.
Where is as_view() defined?
as_view() is defined in src/flask/views.py at line 86.

Analyze Your Own Codebase

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

Try Supermodel Free