Django Class based views Class Based Views

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.

views.py

from django.views.generic import TemplateView


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

urls.py

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.



Got any Django Question?