Skip to content

Commit be308dd

Browse files
committed
documented the base class based views
1 parent b129252 commit be308dd

File tree

2 files changed

+51
-0
lines changed
  • django_async_extensions/aviews/generic
  • docs/views/async-class-based-views

2 files changed

+51
-0
lines changed

django_async_extensions/aviews/generic/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,18 @@ async def options(self, request, *args, **kwargs):
111111

112112

113113
class AsyncTemplateView(TemplateResponseMixin, ContextMixin, AsyncView):
114+
"""
115+
Render a template. Pass keyword arguments from the URLconf to the context.
116+
"""
117+
114118
async def get(self, request, *args, **kwargs):
115119
context = self.get_context_data(**kwargs)
116120
return self.render_to_response(context)
117121

118122

119123
class AsyncRedirectView(AsyncView, RedirectView):
124+
"""Provide a redirect on any GET request."""
125+
120126
async def get(self, request, *args, **kwargs):
121127
return super().get(request, *args, **kwargs)
122128

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## AsyncView
2+
3+
async CBVs are supported via the `AsyncView` class.
4+
5+
```python
6+
from django_async_extensions.aviews.generic import AsyncView
7+
8+
9+
class MyView(AsyncView):
10+
pass
11+
```
12+
13+
the `AsyncView` works similar to django's [View](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#view) class, with a few differences:
14+
15+
1. `AsyncView.as_view()` returns a coroutine.
16+
2. `AsyncView.dispatch()` is an async function.
17+
3. http handlers (`def get()`, `def post()`) are expected to be async.
18+
19+
20+
## AsyncTemplateView
21+
22+
for easy use an async version of `TemplateView` is available
23+
24+
```python
25+
from django_async_extensions.aviews.generic import AsyncTemplateView
26+
27+
class MyTemplateView(AsyncTemplateView):
28+
template_name = "template.html"
29+
```
30+
31+
`AsyncTemplateView` works like django's [TemplateView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#templateview) except that the `get()` method is async.
32+
33+
34+
## AsyncRedirectView
35+
36+
an async version of `RedirectView` is also available
37+
38+
```python
39+
from django_async_extensions.aviews.generic import AsyncRedirectView
40+
41+
class ThisRedirectView(AsyncRedirectView):
42+
pattern_name = "that-view"
43+
```
44+
45+
`AsyncRedirectView` works like django's [RedirectView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#redirectview) except that all http methods are async.

0 commit comments

Comments
 (0)