Skip to content

adamghill/django-fbv

Repository files navigation

django-fbv 🕶️

Utilities to make Django function-based views cleaner, more efficient, and better tasting. 💥

PyPI PyPI - Downloads GitHub Sponsors

📖 Complete documentation: https://django-fbv.adamghill.com

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}
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 🖼️

About

Utilities to make function-based views cleaner, more efficient, and better tasting.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published