django generic views code example

Example 1: set the context data in django listview

from django.views.generic import DetailView
from books.models import Publisher

class PublisherDetail(DetailView):

    context_object_name = 'publisher'
    queryset = Publisher.objects.all()

Example 2: django listview

class YourView(ListView):
	model				= YourModel
    paginate_by			= your_pagination_number
    context_object_name = 'your_model_in_plural'
    template_name 		= 'your_list.html'

Example 3: create views django

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hola(request):
    return HttpResponse("Hola Mundo, Esto es Django")

Example 4: import generic

from django.views.generic import (
  	CreateView,
  	ListView,
  	DetailView,
  	UpdateView,
  	DeleteView,
	TemplateView,
)

Example 5: get context data django

class PublisherDetail(DetailView):

    model = Publisher

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in a QuerySet of all the books
        context['book_list'] = Book.objects.all()
        return context