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 ?