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:
- 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:
- Create an project
- Set up app inside project folder.
- Simple class for CBV which returns a plain text.
- Setup URL for that view.
- 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:
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>")
- 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
Download source code
So, it is all about Django Class Based Views. We will discuss more about that in our further tutorials. Thanks!!!