> For a class-based view, this means making its __call__() method an async
def (not its __init__() or as_view()).
This isn't really appropriate for Django's class-based views:
* We don't implement `__call__()`, rather going via `as_view() — for a
per-request instance — and then `dispatch()`.
* Users expect to implement the HTTP method handlers — `get`, `post`, and
so on — rather than these ''more internal'' bits.
Ideally we'd allow specifying `async def` at the method handler level, to
allow using `await` in the handler, and writing views such as this:
{{{
import asyncio
from django.http import HttpResponse
from django.views import View
class AsyncView(View):
async def get(self, request, *args, **kwargs):
# Perform io-blocking view-logic using await, sleep for
example.
await asyncio.sleep(1)
return HttpResponse("Hello async world!")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33611>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: Andrew Godwin (added)
Comment:
[https://github.com/django/django/pull/15559 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:1>
* component: HTTP handling => Generic views
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:2>
Old description:
> The current [https://docs.djangoproject.com/en/4.0/topics/async/#async-
> views topic docs for Async views] say this about class-based views:
>
> > For a class-based view, this means making its __call__() method an
> async def (not its __init__() or as_view()).
>
> This isn't really appropriate for Django's class-based views:
>
> * We don't implement `__call__()`, rather going via `as_view() — for a
> per-request instance — and then `dispatch()`.
> * Users expect to implement the HTTP method handlers — `get`, `post`, and
> so on — rather than these ''more internal'' bits.
>
> Ideally we'd allow specifying `async def` at the method handler level, to
> allow using `await` in the handler, and writing views such as this:
>
> {{{
> import asyncio
> from django.http import HttpResponse
> from django.views import View
>
> class AsyncView(View):
> async def get(self, request, *args, **kwargs):
> # Perform io-blocking view-logic using await, sleep for
> example.
> await asyncio.sleep(1)
> return HttpResponse("Hello async world!")
> }}}
New description:
The current [https://docs.djangoproject.com/en/4.0/topics/async/#async-
views topic docs for Async views] say this about class-based views:
> For a class-based view, this means making its `__call__()` method an
async def (not its `__init__()` or `as_view()`).
This isn't really appropriate for Django's class-based views:
* We don't implement `__call__()`, rather going via `as_view()` — for a
per-request instance — and then `dispatch()`.
* Users expect to implement the HTTP method handlers — `get()`, `post()`,
and so on — rather than these ''more internal'' bits.
Ideally we'd allow specifying `async def` at the method handler level, to
allow using `await` in the handler, and writing views such as this:
{{{
import asyncio
from django.http import HttpResponse
from django.views import View
class AsyncView(View):
async def get(self, request, *args, **kwargs):
# Perform io-blocking view-logic using await, sleep for
example.
await asyncio.sleep(1)
return HttpResponse("Hello async world!")
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:3>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:4>
* owner: nobody => Carlton Gibson
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:5>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:6>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"9ffd4eae2ce7a7100c98f681e2b6ab818df384a4" 9ffd4ea]:
{{{
#!CommitTicketReference repository=""
revision="9ffd4eae2ce7a7100c98f681e2b6ab818df384a4"
Fixed #33611 -- Allowed View subclasses to define async method handlers.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:8>