You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/middleware/decorate_views.md
+27-6Lines changed: 27 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,6 @@ they work almost exactly like django's [decorator_from_middleware](https://docs.
6
6
and [decorator_from_middleware_with_args](https://docs.djangoproject.com/en/5.1/ref/utils/#django.utils.decorators.decorator_from_middleware_with_args)
7
7
but it expects an async middleware as described in [AsyncMiddlewareMixin](base.md)
8
8
9
-
**Important:** if you are using a middleware that inherits from [AsyncMiddlewareMixin](base.md) you can only decorate async views
10
-
if you need to decorate a sync view change middleware's `__init__()` method to accept async `get_response` argument.
11
-
12
9
with an async view
13
10
```python
14
11
from django.http.response import HttpResponse
@@ -30,12 +27,18 @@ async def my_view(request):
30
27
```
31
28
32
29
33
-
if you need to use a sync view design your middleware like this
30
+
if your view is sync, it'll be wrapped in `sync_to_async` before getting passed down to middleware.
31
+
32
+
if you need, you can disable this by passing `async_only=False`.
33
+
note that the middlewares presented in this package will error if you do that, so you have to override the `__init__()` and `__call__()` methods to handle that.
0 commit comments