Skip to content
Related Articles

Related Articles

FormView – Class Based Views Django

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Jul, 2022
View Discussion
Improve Article
Save Article

FormView refers to a view (logic) to display and verify a Django Form. For example, a form to register users at Geeksforgeeks. Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views: 

  • The organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching.
  • Object-oriented techniques such as mixins (multiple inheritances) can be used to factor code into reusable components.

Class-based views are simpler and more efficient to manage than function-based views. A function-based view with tons of lines of code can be converted into a class-based view with few lines only. This is where Object Oriented Programming comes into impact. 

Django FormView – Class-Based Views

Illustration of How to create and use FormView using an Example. Consider a project named Core having an app named books. 
 

Refer to the following articles to check how to create a project and an app in Django. 
 

Folder Structure

 

Stepwise Implementation to create Class-Based Views

Step 1: Create a basic Project

Create a project by following the command

django-admin startproject core
  • A New Folder with the name project name will be created.

Step 2: Creating an App in Django

To create a basic app in your Django project you need to go to the directory containing manage.py and from there enter the command:

python manage.py startapp books

Step 3: Go to core/URLs and set the path for our app.

 

Step 4: Go to core/setting and register our app name “books“.

 

Step 5: Paste this to your books/admin.py

from django.contrib import admin
from . import models


@admin.register(models.Books)
class AuthorAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',), }

Step 6: Create a model in books/models.py.

 

Step 7: After you have a model, Let’s create a form in which we will be creating CreateView. In books/forms.py.

 

Step 8: After creating the form let’s create CreateView in geeks/views.py, 

 

Step 9: Map a URL to this view in books/urls.py.

 

Step 10: Set Urls for books/urls.py

from django.urls import path
from .views import AddBookView

app_name = 'books'

urlpatterns = [
    path('', AddBookView.as_view(), name='add'),
]

Step 11: Create a template for this view in books/add.html, 

html




<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="container" style="max-width:600px">
        <div class="px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
            <h1 class="display-4">Welcome to GFG Class Based Views Django</h1>
         
        </div>
        <div class="py-5">
            <div class="row">
                <div class="col-12">
                    <form method="post">
                        {% csrf_token %}
                        {{ form.as_p }}
                        <input type="submit">
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>


Step 12: Now run your app by the following command.

python manage.py runserver

Now visit http://127.0.0.1:8000/
 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!