Skip to content

Commit e891c95

Browse files
committed
documented the form class based view
1 parent 179f894 commit e891c95

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## Generic editing views
2+
3+
### AsyncFormView
4+
5+
`AsyncFormView` works like django's [FormView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-editing/#formview)
6+
but the inheritance tree has changed to work asynchronously:
7+
8+
* [TemplateResponseMixin](https://docs.djangoproject.com/en/5.1/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin)
9+
* [AsyncBaseFormView](...)
10+
* [AsyncFormMixin](mixins-editing.md#asyncformmixin)
11+
* [AsyncProcessFormMixin](mixins-editing.md#asyncprocessformview)
12+
* [AsyncView](base.md#asyncview)
13+
* [View](https://docs.djangoproject.com/en/5.1/ref/class-based-views/base/#django.views.generic.base.View)
14+
15+
16+
*Example myapp/forms.py*
17+
```python
18+
from django import forms
19+
20+
class ContactForm(forms.Form):
21+
name = forms.CharField()
22+
message = forms.CharField(widget=forms.Textarea)
23+
24+
def send_email(self):
25+
# send some email
26+
pass
27+
```
28+
29+
*Example myapp/views.py*
30+
```python
31+
from myapp.forms import ContactForm
32+
from django_async_extensions.aviews.generic.edit import AsyncFormView
33+
34+
35+
class ContactFormView(AsyncFormView):
36+
template_name = "contact.html"
37+
form_class = ContactForm
38+
success_url = "/thanks/"
39+
40+
async def form_valid(self, form):
41+
# This method is called when valid form data has been POSTed.
42+
# It should return an HttpResponse.
43+
form.send_email()
44+
return await super().form_valid(form)
45+
```
46+
47+
*Example myapp/contact.html*
48+
```html
49+
<form method="post">{% csrf_token %}
50+
{{ form.as_p }}
51+
<input type="submit" value="Send message">
52+
</form>
53+
```
54+
55+
## Base Class
56+
57+
### AsyncBaseFormView
58+
A base view for displaying a form. It is not intended to be used directly,
59+
but rather as a parent class of the django_async_extensions.aviews.generic.edit.AsyncFormView or other views displaying a form.
60+
61+
similar to django's [BaseFormView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/generic-editing/#django.views.generic.edit.BaseFormView)
62+
but the ancestors are different:
63+
64+
* [AsyncFormMixin](mixins-editing.md#asyncformmixin)
65+
* [AsyncProcessFormMixin](mixins-editing.md#asyncprocessformview)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## AsyncFormMixin
2+
3+
A mixin class that provides facilities for creating and displaying forms.
4+
5+
`AsyncFormMixin` is similar to django's [FormMixin](https://docs.djangoproject.com/en/5.1/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin)
6+
with a number of differences:
7+
8+
1. `get_form_class()` method is async.
9+
2. `get_form()` method is async.
10+
3. `form_valid()` method is async.
11+
4. `form_invalid()` method is async.
12+
5. `get_context_data()` is async.
13+
14+
15+
## AsyncModelFormMixin
16+
A form mixin that works on ModelForms, rather than a standalone form.
17+
18+
`AsyncModelFormMixin` similar to django's [ModelFormMixin](https://docs.djangoproject.com/en/5.1/ref/class-based-views/mixins-editing/#modelformmixin)
19+
with a number of differences:
20+
21+
1. `AsyncModelFormMixin` inherits from [AsyncFormMixin](mixins-editing.md#asyncformmixin) and [AsyncSingleObjectMixin](detail.md#asyncsingleobjectmixin) so anything mentioned on those classes also applies here.
22+
2. `get_form_class()` method is async.
23+
24+
## AsyncProcessFormView
25+
A mixin that provides basic HTTP GET and POST workflow.
26+
27+
`AsyncProcessFormView` works similar to django's [ProcessFormView](https://docs.djangoproject.com/en/5.1/ref/class-based-views/mixins-editing/#processformview),
28+
but it inherits from [AsyncView](base.md#asyncview) and all the http methods are async.

0 commit comments

Comments
 (0)