Class-Based Views Django – Complete Guide | Django Tutorials

Class-based views Django copy
Django 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

Sometimes, Django beginners students put a question, whether we use class-based views or functions-based? We say that class-based views are just alternatives of function-based and function-based views are alternatives of class-based views. You can use any one of them. But it depends upon the situation, sometimes you need class base view and sometimes you need FBV. You know when you use CBVs in your project, most of the work is done, which means you have to write very little code in case of using CBVs.

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

 

Leave your thought here

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

Recent Comments

  1. Push it to the limit cool Wolf! You are the best and you can do everything! It'll all work out very very very soon! https://www.samsung.com smkmkplobydlmcrjmzgvx 2396134 on Django Create superuser | Django Tutorial
  2. Thank you For your hard work over the years! For this, we give you the opportunity. https://google.com#1234567890 For more information, see the instructions. skfhjvkjsdjsrbhvbsrfhkis 9357930 on Django Create superuser | Django Tutorial
  3. Thank you For your hard work over the years! For this, we give you the opportunity. https://google.com#1234567890 For more information, see the instructions. skfhjvkjsdjsrbhvbsrfhkis 5948210 on Django Create superuser | Django Tutorial
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Get your Enrollment

50%OFF

Leave your details below and receive a discount coupon in your inbox