views.generic.edit.FormView

class django.views.generic.edit.FormView

A view that displays a form. On error, redisplays the form with validation errors; on success, redirects to a new URL.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Example myapp/forms.py:

1
2
3
4
5
6
7
8
9
from django import forms
 
class ContactForm(forms.Form):
    name = forms.CharField()
    message = forms.CharField(widget=forms.Textarea)
 
    def send_email(self):
        # send email using the self.cleaned_data dictionary
        pass

Example myapp/views.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
from myapp.forms import ContactForm
from django.views.generic.edit import FormView
 
class ContactView(FormView):
    template_name = 'contact.html'
    form_class = ContactForm
    success_url = '/thanks/'
 
    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        form.send_email()
        return super(ContactView, self).form_valid(form)

Example myapp/contact.html:

1
2
3
4
<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Send message" />
</form>
doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.