Journal Entry · 2026.08.06 2 min read
← All journal entries

2 min read

Getting started with class-based views

I use ListView, DetailView, and FormView across this site, and switch to plain function views for custom endpoints.

Class-based views (CBVs) in Django hide control flow behind inheritance chains, so I stick to four generic views that match standard pages: ListView, DetailView, TemplateView, and FormView.

Generic Views That Cut Boilerplate

Public pages on this portfolio follow three shapes:

  • ListView for collections: I use ProjectListView and BlogListView for queryset filtering, search queries, and pagination without hand-writing page math.
  • DetailView for slug lookups: I use ProjectDetailView and PostDetailView to resolve <slug:slug> and return a 404 for missing records or unpublished drafts.
  • FormView for validated POST workflows: I use BriefPageView to render the contact form, bind aria-describedby error hooks on invalid input, and redirect after a valid POST so a browser refresh does not send a duplicate message.
class PostDetailView(DetailView):
    template_name = "core/post_detail.html"
    context_object_name = "post"

    def get_queryset(self):
        # Drafts must 404, not leak via direct URL.
        return Post.objects.filter(published=True)

Plain Function Views

Generic views get in the way on non-template endpoints. For JSON handlers like brief_submit, plain text like robots_txt, or file streaming in media_file, I write a Python function with @require_GET or @require_POST so you can read it top to bottom.

Reach for a generic CBV to remove repeated queryset, pagination, or form code. Switch to a function view the moment you fight its lifecycle methods.

Mark Ocampos
Independent web engineer
Have a project in mind?
Send me a message.