Introduction

django-fbv includes utilities to make function-based views cleaner, more efficient, and better tasting. 💥

Why? 🤔

The Django community has two ways to write views: class-based and function-based. One benefit of function-based views is that the HttpRequest input and the HttpResponse output is explicit, instead of being hidden within a hierarchy of classes and mixins.

django-fbv reduces the boilerplate code required when using function-based views. It also leverages locality of behavior – the name of the template is clearly associated to the view, instead of being part of the return statement.

Instead of this:

# sample_app/views.py
from django.shortcuts import render

def regular_function_based_view(request):
  return render(request, "template_name.html", {"data": 123})

You can write this:

# sample_app/views.py
from fbv.decorators import render_html

@render_html("template_name.html")
def django_fbv_view(request):
  return {"data": 123}

Note

If you want a more detailed critique of class-based views, I recommend reading https://spookylukey.github.io/django-views-the-right-way/.

Installation ⚙️

pip install django-fbv OR uv add django-fbv

The decorators and views can be used by just importing them. The middleware needs to be installed.

Features 🤩

Decorators

Views

Middleware

Prior art 🖼️