A tool to help inspect all class-based views within your Django project 🔎 ✨
Inspired by django-debug-toolbar ❤️- Install with pip
pip install django-cbv-inspect
- Add
cbv_inspect
to your list ofINSTALLED_APPS
in your Django settings module
INSTALLED_APPS = [
...
"cbv_inspect",
...
]
- Add the middleware to your list of
MIDDLEWARE
classes in your Django settings module
MIDDLEWARE = [
...
"cbv_inspect.middleware.DjCbvInspectMiddleware",
...
]
- Make sure your
TEMPLATES
settings uses theDjangoTemplates
backend withAPP_DIRS
set toTrue
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"APP_DIRS": True,
...
}
]
When all installation steps are done, any html response rendered by a class-based view should display the django-cbv-inspect
toolbar on the page.
By default, all class-based views will be processed by the middleware. If you wish to exclude views, there are two options:
from cbv_inspect.mixins import DjCbvExcludeMixin
class MyCoolView(DjCbvExcludeMixin, View):
pass
from django.utils.decorators import method_decorator
from cbv_inspect.decorators import djcbv_exclude
@method_decorator(djcbv_exclude, name="dispatch")
class MyCoolView(View):
pass
You can run the example
project locally to test things out!
Clone the project and from the root of the repo, run the following Make command to setup the example
project:
make run-example
To run unittests with coverage, run
make coverage
The django-cbv-inspect
toolbar has three main sections:
- View information
- CBV method call chain
- MRO classes
This section shows high level information about the class-based view, request, and url.
This is the main section that shows all methods that were excuted for the current class-based view:
It shows:
- method name and signature
- Classy Class-Based Views (ccbv.co.uk) links
- method arguments and return value
- all resolved
super()
calls defined in the method - module location
This section lists all MRO classes of the current class-based view class.
This can come in handy especially with the prior section when mapping the execution of a class-based view.
Django class-based views are hard to grasp especially when you're new to Django.
Fortunately for us, tools like django-debug-toolbar and ccbv.co.uk are super helpful in providing extra context for debugging.
My goal for this app was to take what goes on under the hood in a class-based view and display it in an easy to use interface, just like what django-debug-toolbar does.
Hopefully this can help debug your class-based views!
Happy coding! ✨