Use Class Based approach for Views

Hi everyone !

I’ve read the following pages about Django’s Class Based Approach :

https://docs.djangoproject.com/fr/3/topics/class-based-views/intro/

When I went searching about developper’s preferences regarding this matter, it seems that there is no “best way” for writing views, it is just up to the developper.

In my case, I like function based views but also class based views like this (without generic stuffs) :

from django.shortcuts import render
from django.views import View
from .models import ProductModel
from .forms import ProductForm

# Create your views here.
class IndexView(View):
template_name = "products/index.html"
context = {
    "ProductForm":ProductForm,
    "ProductModel":ProductModel.objects.all()
}

def get(self, request):
    return render(request, self.template_name, self.context)

def post(self, request):
    form = ProductForm(request.POST)
    if(form.is_valid()):
        form.save()
    else:
        form = ProductForm()

    return render(request, self.template_name, self.context)

The issue here is that everything works perfectly but I have to manually reload the page in order to see the changes on screen whereas with the function based approach :

def index(request):
template_name = "products/index.html"
context = {
    "ProductForm":ProductForm.objects.all(),
    "ProductModel":ProductModel.objects.all()
}

if(request.method == "POST"):
    form = ProductForm(request.POST)

    if(form.is_valid()):
        form.save()
    else:
        form = ProductForm()

return render(request, template_name, context)

it worked automatically, do you have an idea why ?

Briefly, you can’t set your context at the class level. Setting a variable at the class level means that the value is set when the class is defined.

When you’re working with class-based views, you will need to learn what functions exist that you can override to set values during the processing of the view.

First, I’m going to suggest that you don’t inherit directly from View. There are plenty of views where you can find the most appropriate starting point. In your case, I’m going to suggest starting from a FormView.

Now, normally I recommend the standard Django documentation for most common questions, but in this case, I’m also going to recommend the “Classy Class-Based View” site as an aid to understanding how these views work.

Briefly, you’re going to want to look at the get_context_data function within the View as a point at which you can inject the context you want to display.

Also, if you’re not currently comfortable with Python classes, this would be a really good time to learn about how they work - particularly in terms of class inheritance and the python method-resolution order (MRO).

It’s for reasons like this that I suggest beginners stick to FBV’s for a while. I still prefer using them in many cases.

Another thing to plug is django-vanilla-views. This has a set of simpler CBV’s than Django’s built-in ones, and I prefer using this when doing CBV’s.

1 Like

Hi guys ! Thank you for your answers !

Yes, I think that I will stick to FBV for a while in order to understand other concepts better :slight_smile:

+1 on the django-vanilla-views - wasn’t thinking about them when I was writing my response. Good plug!