Table of contents
1.
Introduction
2.
FormView in Django
3.
Class-Based Views
4.
Validate information in the form of View
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Aug 13, 2025

FormView | Django

Author Juhi Sinha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Django python framework is well-known throughout the world. Developers' lives have been made easier to create a full-fledged web application with this framework. Django provides several class-based generic views to accomplish everyday tasks. One among them is FormView.

In this article, we will learn FormView in Django. So, without any further ado, let's get started!

FormView in Django

FormView refers to a view (logic) to display and verify a Django Form. FormView should be used when we need a form on the page and want to perform specific actions when a valid form is submitted.

Views can be implemented as Python objects rather than functions when using class-based views. Class-based views are more efficient and easier to manage than function-based views. With only a few lines of code, a function-based view with a large number of lines of code can be converted to a class-based view. Object-Oriented Programming (OOP) is used in this situation.

In the following ways, FormView is superior to other views such as vanilla View:

  • Avoid boilerplate code.
  • Better concern separation.
  • Code that is more concise and maintainable.

Class-Based Views

Using an example, let us see how to create and use FormView. Consider the coding project. Ninjas have their app, dubbed ninjas.

After creating a project and an app, let’s make a form from which we'll create FormView. In the file ninjas/forms.py,

from django import forms

class NinjasForm(forms.Form):
     title = forms.CharField()
   description = forms.CharField(widget = forms.Textarea)
You can also try this code with Online Python Compiler
Run Code

 

Let's make a FormView after we've created the form in ninjas/views.py,

from django.views.generic.edit import FormView

from .forms import NinjasForm

class NinjasFormView(FormView):
     form_class = NinjasForm
   
     template_name = "ninjas /ninjasmodel_form.html"

     success_url ="/thanks/"
You can also try this code with Online Python Compiler
Run Code

 

In ninjas/ninjasmodel form.html, create a template for this view,

<form method="post">
   {% csrf_token %}
   {{ form.as_p }}
   <input type="submit" value="Save">
</form>

 

In ninjas/urls.py, create a URL for this view.

from django.urls import path

from .views import NinjasFormView
urlpatterns = [
   path('', NinjasFormView.as_view()),
]
You can also try this code with Online Python Compiler
Run Code

Validate information in the form of View

Validating data in a form is easy with Class-Based Views' built-in functions. In the ninjas/views.py file,


from django.views.generic.edit import FormView

from .forms import NinjasForm

class NinjasFormView(FormView):
 
   form_class = NinjasForm
 
   template_name = "ninjas / ninjasmodel_form.html"
   success_url ="/thanks/"

   def form_valid(self, form):
         print(form.cleaned_data)
       return super().form_valid(form)
You can also try this code with Online Python Compiler
Run Code

 

In the form_valid function, we can perform the desired functionality. It prints the data in our case.

The above code will create a form template. We can enter our data in the form. Let’s check it in the browser:

If we check our terminal, then it will print our details.

Frequently Asked Questions

  1. What is FormView in Django?
    In Django, a FormView view (logic) displays and verifies a Django Form. For example, there is a form for registering users. We can implement Views as Python objects rather than functions with the help of class-based views.
     
  2. What is Django's Form class?
    In Django Class-Based, the 'form class' is used to represent the form used in a class-based view.
     
  3. What is the purpose of FormView?
    The FormView control is used to show a single record from a data source at a time. The FormView control allows us to create templates for displaying and editing data-bound values. Controls, binding expressions, and formatting define the look and functionality of the form in the templates.

Key Takeaways

In this article, we've learned about FormView in Django. You can also check our previous articles on Django like Django FormsRender HTML Forms in Django, Django Models, and Django Template.

If you are curious to learn more about front-end web development, Coding Ninjas is here with one of the best courses available, which you can find here

Thanks for reading!

Live masterclass