CBV - FormView

From Classy Class Based Views FormView

A view for displaying a form and rendering a template response.


The only new attribute to review this time is form_class. That being said, there are a few implementation details to cover

  • form_class: takes a Form class and is used to render the form on the html template later on.


Up to this point we haven’t really needed to override a method to get any of the views to work. This time though, we need someway for the view to verify that the data is valid and then save it somewhere.

  • form_valid: used to verify that the data entered is valid and then saves to the database. Without this method your form doesn’t do anything


This example is a bit more than previous examples. A new file called forms.py is used to define the form that will be used.


from django.forms import ModelForm
from rango.models import Person

class PersonForm(ModelForm):
    class Meta:
        model = Person
        exclude = [


class myFormView(FormView):
    form_class = PersonForm
    template_name = 'rango/person_form.html'
    extra_context = {
        'type': 'Form'
    success_url = reverse_lazy('rango:list_view')

    def form_valid(self, form):
        person = Person.objects.create(
        return super(myFormView, self).form_valid(form)


path('form_view/', views.myFormView.as_view(), name='form_view'),


    <h3>{{ type }} View</h3>
    {% if type != 'Update' %}
        <form action="." method="post">
    {% else %}
        <form action="{% url 'rango:update_view' object.id %}" method="post">
    {% endif %}
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">SUBMIT</button>


A visual representation of how FormView is derived can be seen here:



I really struggled with understanding why you would want to implement FormView. I found this explanation on Agiliq and it helped me grok the why:

FormView should be used when you need a form on the page and want to perform certain action when a valid form is submitted. eg: Having a contact us form and sending an email on form submission.

CreateView would probably be a better choice if you want to insert a model instance in database on form submission.

While my example above works, it’s not the intended use of FormView. Really, it’s just an implementation of CreateView using FormView