Django Class based views Class Based Views


Class based views let you focus on what make your views special.

A static about page might have nothing special, except the template used. Use a TemplateView! All you have to do is set a template name. Job done. Next.

from django.views.generic import TemplateView

class AboutView(TemplateView):
    template_name = "about.html"

from django.conf.urls import url
from . import views

urlpatterns = [
    url('^about/', views.AboutView.as_view(), name='about'),

Notice how we don't use directly AboutView in the url. That's because a callable is expected and that's exactly what as_view() return.