Not understanding with generic views

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic

from .models import Choice, Question

class IndexView(generic.ListView):
template_name = ‘polls/index.html’
context_object_name = ‘latest_question_list’

def get_queryset(self):
    """Return the last five published questions."""
    return Question.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
model = Question
template_name = ‘polls/detail.html’

class ResultsView(generic.DetailView):
model = Question
template_name = ‘polls/results.html’

def vote(request, question_id):
… # same as above, no changes needed.

This code i have copied from django documentation. Here the question is:
Using generic views we shouldnt write any function to handel request ie. POST,GET etc. Am i correct!!! ???

Yes, that’s essentially correct, unless you need to modify the standard behavior of the class-provided methods.

That’s the beauty / advantage of the class-based structure. When you inherit a class, you inherit its methods. Since the DetailView already provides a get method, you don’t need to.

For a better understanding of how the CBVs work, there are a couple of 3rd party documentation sites that I’ve found to be extremely helpful - The Classy Class Based View site and Brennan Tymrak’s Django Class-Based Views in Diagrams page.

1 Like