MCPcopy
hub / github.com/pallets/flask / as_view

Method as_view

src/flask/views.py:86–135  ·  view source on GitHub ↗

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_req

(
        cls, name: str, *class_args: t.Any, **class_kwargs: t.Any
    )

Source from the content-addressed store, hash-verified

84
85 @classmethod
86 def as_view(
87 cls, name: str, *class_args: t.Any, **class_kwargs: t.Any
88 ) -> ft.RouteCallable:
89 """Convert the class into a view function that can be registered
90 for a route.
91
92 By default, the generated view will create a new instance of the
93 view class for every request and call its
94 :meth:`dispatch_request` method. If the view class sets
95 :attr:`init_every_request` to ``False``, the same instance will
96 be used for every request.
97
98 Except for ``name``, all other arguments passed to this method
99 are forwarded to the view class ``__init__`` method.
100
101 .. versionchanged:: 2.2
102 Added the ``init_every_request`` class attribute.
103 """
104 if cls.init_every_request:
105
106 def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
107 self = view.view_class( # type: ignore[attr-defined]
108 *class_args, **class_kwargs
109 )
110 return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
111
112 else:
113 self = cls(*class_args, **class_kwargs) # pyright: ignore
114
115 def view(**kwargs: t.Any) -> ft.ResponseReturnValue:
116 return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
117
118 if cls.decorators:
119 view.__name__ = name
120 view.__module__ = cls.__module__
121 for decorator in cls.decorators:
122 view = decorator(view)
123
124 # We attach the view class to the view function for two reasons:
125 # first of all it allows us to easily figure out what class-based
126 # view this thing came from, secondly it's also used for instantiating
127 # the view class so you can actually replace it with something else
128 # for testing purposes and debugging.
129 view.view_class = cls # type: ignore
130 view.__name__ = name
131 view.__doc__ = cls.__doc__
132 view.__module__ = cls.__module__
133 view.methods = cls.methods # type: ignore
134 view.provide_automatic_options = cls.provide_automatic_options # type: ignore
135 return view
136
137
138class MethodView(View):

Callers 15

_async_appFunction · 0.80
test_basic_viewFunction · 0.80
test_method_based_viewFunction · 0.80
test_view_patchingFunction · 0.80
test_view_inheritanceFunction · 0.80
test_view_decoratorsFunction · 0.80
test_implicit_headFunction · 0.80
test_explicit_headFunction · 0.80
test_endpoint_overrideFunction · 0.80

Calls 1

decoratorFunction · 0.70

Tested by 15

_async_appFunction · 0.64
test_basic_viewFunction · 0.64
test_method_based_viewFunction · 0.64
test_view_patchingFunction · 0.64
test_view_inheritanceFunction · 0.64
test_view_decoratorsFunction · 0.64
test_implicit_headFunction · 0.64
test_explicit_headFunction · 0.64
test_endpoint_overrideFunction · 0.64