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
)
| 84 | |
| 85 | @classmethod |
| 86 | def as_view( |
| 87 | cls, name: str, *class_args: t.Any, **class_kwargs: t.Any |
| 88 | ) -> ft.RouteCallable: |
| 89 | class="st">"""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 | class="st">""" |
| 104 | if cls.init_every_request: |
| 105 | |
| 106 | def view(**kwargs: t.Any) -> ft.ResponseReturnValue: |
| 107 | self = view.view_class( class="cm"># type: ignore[attr-defined] |
| 108 | *class_args, **class_kwargs |
| 109 | ) |
| 110 | return current_app.ensure_sync(self.dispatch_request)(**kwargs) class="cm"># type: ignore[no-any-return] |
| 111 | |
| 112 | else: |
| 113 | self = cls(*class_args, **class_kwargs) class="cm"># pyright: ignore |
| 114 | |
| 115 | def view(**kwargs: t.Any) -> ft.ResponseReturnValue: |
| 116 | return current_app.ensure_sync(self.dispatch_request)(**kwargs) class="cm"># 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 | class="cm"># We attach the view class to the view function for two reasons: |
| 125 | class="cm"># first of all it allows us to easily figure out what class-based |
| 126 | class="cm"># view this thing came from, secondly it's also used for instantiating |
| 127 | class="cm"># the view class so you can actually replace it with something else |
| 128 | class="cm"># for testing purposes and debugging. |
| 129 | view.view_class = cls class="cm"># type: ignore |
| 130 | view.__name__ = name |
| 131 | view.__doc__ = cls.__doc__ |
| 132 | view.__module__ = cls.__module__ |
| 133 | view.methods = cls.methods class="cm"># type: ignore |
| 134 | view.provide_automatic_options = cls.provide_automatic_options class="cm"># type: ignore |
| 135 | return view |
| 136 | |
| 137 | |
| 138 | class MethodView(View): |