Django Class Based Views

Django Class Based Views “CBV”

Django Class Based Views are most common functionality of Django. Till now, we have discussed a lot about function based views. In this tutorial we will discuss about Class Based Views in Django. Here are some important points about Django Class Based Views:

Django-class-based-views
Django-class-based-views
  • However Django provides really powerful tools to use OOP and classes to define views.
  • The CBV offers great functionality and for most experienced users of Django, CBVs are their default choice for creating views.
  • It is much easier to understand CBV after working with function views!
  • A view with class is more secure and structured.

Hello world using CBV

Now, we will write simple program to understand Django Class Based Views. Here is the complete process i.e. we are going to do:

  1. Create an project
  2. Set up app inside project folder.
  3. Simple class for CBV which returns a plain text.
  4. Setup URL for that view.
  5. Run server and check output.

1. Create an project

Create an project for practice CBVs (Ignore if you already have. ). I am going to create project named “CBV“. Here is the command for doing so :

django-admin startapp AppName

2. Setup app inside project folder

Now let’s create an app inside project folder. Add app to INSTALLED_APPS list of the project’s settings. I am going to create an app named cbvApp. Here are the set of commands for doing so:

django-admin startapp AppName
or
python manage.py startapp AppName

Here is my directory tree:

Django Class Based Views
Django Class Based Views

CBV > CBV > settings.py

INSTALLED_APPS = [
    ..............
    'cbvapp',
]

3. Simple class for CBV which returns a plain text

CBV > cbvapp > views.py

from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse

class MyCBV(View):
    def get(self, request):
        return HttpResponse("<h1>Hello World!! It is My First CBV !!!</h1>")

Explanation

  • First of all, we need to import Django’s built in View from generic module.
  • Httpresponse function is to return plain text.
  • Next their is a child class MyCBV inherited from base class View.
  • Next here is get() method which returns an simple HttpResponse.

4. Setup URL for that view

Now everything is setup successfully. What we should do next is to setup URL for that view. Entire process i.e. setting views and URLs is same as in function based views. Moreover, the slight difference is defining the view inside the class rather than simple function.

  • We will also have to slightly change the way we call a class based view in the urls.py file of our project.
  • We need to add in a .as_view() call off the class, this is an inherited method from the View we mentioned earlier.
  • Here is the code inside the urls.py file:

CBV > CBV > urls.py

from django.contrib import admin
from django.urls import path
from cbvapp import views
urlpatterns = [
    path('',views.MyCBV.as_view()),
    path('admin/', admin.site.urls),
]

5. Run server and check output

So now, everything is setup. Check output for the code using:

python manage.py runserver
Django Class Based Views
Django Class Based Views

Download source code

Download project on Github
Download project on Github 

So, it is all about Django Class Based Views. We will discuss more about that in our further tutorials. Thanks!!!

7 thoughts on “Django Class Based Views

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. I just want to mention I’m beginner to weblog and definitely enjoyed you’re page. More than likely I’m likely to bookmark your blog . You really have incredible well written articles. Thank you for sharing with us your blog site.

  2. I spent four years trying EVERYTHING in Online Dating, and through a huge amount of trial and error, I produced a system that I will share for you. This book will take you, step by step, through everything you need to know to double, triple or even quadruple the number of women you meet online.

  3. Absolutely NEW update of captcha solution package “XEvil 4.0”:
    captchas solving of Google (ReCaptcha-2 and ReCaptcha-3), Facebook, BitFinex, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another categories of captcha,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can use XEvil 4.0 with any most popular SEO/SMM programms: iMacros, XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? You can find a lot of demo videos about XEvil in YouTube.

    FREE DEMO AVAILABLE!

    See you later!

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.