|
| 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) |
0 commit comments