The single most significant advantage in Django class-based views is inheritance. On a large project, it's likely that we will have lots of similar views. Instead of writing the repeated code we can simply have our views inherit from a base view. Also,Django ships with a collection of generic view classes that can be used to do some of the most common tasks.
1. Template View Function based view urls.pyfrom django.conf.urls import url
from . import views
urlpatterns = [url(r'^about-us/$', views. about_us , name=" about _us"),
]
views.pyfrom django.shortcuts import render
def about_us(request):
return render(request, 'templates/contact.html')
Class based view
urls.pyfrom django.conf.urls import url
from .views import AboutUs
urlpatterns = [url(r'^about-us/$', AboutUs.as_view(), name="about_us"),
]
views.py
from django.views.generic import TemplateView
class AboutUs (TemplateView):
template_name = "templates/about.html"
or we can directly write it in "urls.py" urls.pyfrom django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [url(r'^about-us/$', TemplateView.as_view( template_name= "templates/about.html"), name="about_us"),
]
2. Detail View Function based view urls.pyfrom django.conf.urls import url
from . import views
urlpatterns = [ url(r'^author / (?P<pk>[0-9]+)/details/$', views.author_details, name=" author_details "),]
views.pyfrom django.shortcuts import render
from django.shortcuts import get_object_or_404
from books.models import Author
def author_details (request, pk):
author = get_object_or_404( Author, id=pk)
context = {'author': author}
return render(request,
context,
'templates/author_details.html')
Class based view
urls.pyfrom django.conf.urls import url
from .views import AuthorDetails
urlpatterns = [ url(r'^author / (?P<pk>[0-9]+)/details/$', AuthorDetails .as_view() , name="author_details "),]
views.py
from django.views.generic import DetailView
from books.models import Author
class AuthorDetails(DetailView):
model = Author
slug_field = 'pk' # for identifying the object uniquely
context_object_name = 'author' # this name is used access the object inside template (it's optional)
template_name = "templates/
author_details
.html"
we can access object in template as "object" or model name with lowercase letters "modelname" (ex: object.name or author.name )
Note : To send extra context data to the template we can override the super class method get_context_data
from django.views.generic import DetailView
from .models import Author, Book
class AuthorDetails(DetailView):
model = Author
slug_field = 'pk'
template_name = "templates/ author_details .html"
def get_context_data(self, **kwargs):
context = super( AuthorDetails , self).get_context_data(**kwargs)
context['book_list'] = Book.objects.filter(author=self.get_object())return context
3. List View Function based view urls.pyfrom django.conf.urls import url
from . import views
urlpatterns = [url(r'^authors-list/$', views.authors_list, name=" authors_list "),
]
views.pyfrom django.shortcuts import render
from books.models import Author
def authors_list (request):
authors_list = Author.objects.all()
context = {' authors_list' : authors_list }
return render(request,
context,
'templates/
authors_list
.html')
Class based view
urls.pyfrom django.conf.urls import url
from .views import AuthorDetails
urlpatterns = [url(r'^authors-list/$', AuthorsList .as_view() , name=" authors_list "),
]
views.py
from django.views.generic import ListView
from books.models import Author
class AuthorsList(ListView):
model = Author
template_name = "templates/
authors_list
.html"
we can access objects in template as "object_list" or model name with lowercase letters "modelname_list"List view with dynamic filtering
urls.pyfrom django.conf.urls import url
from . import views
urlpatterns = [ url(r'^authors-list/subject/(?P<category>[-\w]+)/$', views.authors_list, name=" authors_list "),]
views.pyfrom django.shortcuts import render
from books.models import Author
def authors_list (request, category ):
authors_list = Author.objects.filter(category=category)
context = {' authors_list' :authors_list,
}return render(request,
context,
'templates/
authors_list
.html')
Class based view
urls.pyfrom django.conf.urls import url
from .views import AuthorsList
urlpatterns = [url(r'^authors-list/subject/(?P<category> )/$', AuthorsList.as_view(), name=" authors_list "),
]
views.py
from django.views.generic import ListView
from books.models import Author
class AuthorsList(ListView):
model = Author
template_name = "templates/ authors_list .html"
paginate_by = 10 # It will paginate the objects such that each page contains atmost 10 objects
def get_queryset(self):
query_set = self.model.objects.filter(category=self.kwargs.get('category