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
-
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.
-
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.
-
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 Forms, Render 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!