Class-Based Views Django – Complete Guide | Django Tutorials
May 21, 2021 2023-04-15 13:38Class-Based Views Django – Complete Guide | Django Tutorials
Class-Based Views Django – Complete Guide | Django Tutorials
Class-Based Views Django
You know Django follows the MVT architectural design pattern. In MVT, V is the views section in which we write logic for web applications or websites. Actually, it works on request and response when the user hits the URL. As you know Django works on two types of views as function base and class base. We have discussed function base views but today we have to discuss class-based views Django.
For class-based views, Django provides a class that is predefined in the Django package. Just we need to call them. There is more functionality that is written in the classes we have to just use in our project. Most of the work is done by just class using in views section.
It is the alternative of funciton based views, it is not replacement of CBVs. It have more advantages than CBVs because it work very fine when you perfrom CRUD operation on any entity. As it have differetn mehtods, properties which work in this way. There are differetn classes which are used for different purposes, all the calses we will discussed in this tutorial, when we update everytime.
More about Class-Based Views Django
All the views classes are inherited from the Views class e.g from django.views
And RedirectView class used to redirect any views to another view. It means when the user wants to get a URL with domainame/product it will be redirected to domainame/about if we used redirect view in that view, So Redirectview provides HTTP redirection.
Sometime, if need, we use view class in the URL pattern.
TemplateView is the class that is derived from generic. This class is used to render a template in the browser for a specific view.
from django.views.generic import TemplateView
urlpatterns = [ path(‘about/’, TemplateView.as_view(template_name=“about.html”)), ]
Class-Based View Django example
from django.views.generic import TemplateView
class AboutView(TemplateView):
template_name = “about.html”
In the above example, there is a class named AboutView which is a child class of TemplateView. It means we are using a view of displaying a template as we render a template in a function-based view. So, we have to pass a class variable name template_name equal to the name of the path of your template that you want to render.
Conclusion
Sure, you have read the tutorial about class-based views Django as we use in Django views.py file to perform different operations. We want to provide the best lesson to our readers, Therefore, we will update our lesson from time to time. So be updated with our blog section at JafriCode.
If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.
After reading today’s topic, if you face any problem while reading, understanding please discuss it with us. If you have understood and have any relevant questions then please discuss with us in the following comment section.
Recomended Django Tutorial for Beginners
What are views in Django | Django Tutorials
How to Render a template in Django
Django Tutorial for Beginners | Django Tutorials series
Complete Django Template Language Tutorial (DTL)
Hack – Django Website Tutorials | Building Complete Website with Django
100% Perfect | How to install Django Step by Step