Django Braces
Django Braces provides a collection of reusable, generic mixins designed to simplify the use of Django's class-based views. These mixins often replicate the functionality of Django's function-based view decorators, addressing common development challenges with class-based views. The library is considered stable and mature, focusing its support on Python versions still receiving fixes and Django LTS releases. The current version is 1.17.0.
Warnings
- breaking Explicit support for Python versions before 3.7 and non-LTS versions of Django was dropped in `django-braces` v1.15.0.
- deprecated The `CreateAndRedirectToEditView` mixin was marked for deprecation since v1.0.0 and subsequently removed in v1.11.0.
- gotcha Prior to v1.9.0, certain access mixins (like `MultipleGroupRequiredMixin` and `GroupRequiredMixin`) could lead to redirect loops for users without appropriate permissions or group memberships.
- gotcha The `LoginRequiredMixin` was rewritten in v1.0 to align with other access mixins. Older implementations might not correctly utilize `login_url`, `redirect_field_name`, or `raise_exception` class attributes.
- gotcha The `PermissionRequiredMixin` will raise an `ImproperlyConfigured` exception if the `permission_required` attribute is not explicitly set on the view class.
- gotcha The `GroupRequiredMixin` assumes that you are using Django's default `Group` model and that your user model has a `groups` ManyToMany relationship. Deviations require overriding `check_membership`.
Install
-
pip install django-braces
Imports
- LoginRequiredMixin
from braces.views import LoginRequiredMixin
- PermissionRequiredMixin
from braces.views import PermissionRequiredMixin
- MultiplePermissionsRequiredMixin
from braces.views import PermissionRequiredMixin (for multiple permissions)
from braces.views import MultiplePermissionsRequiredMixin
- GroupRequiredMixin
from braces.views import GroupRequiredMixin
- CsrfExemptMixin
from braces.views import CsrfExemptMixin
- JSONResponseMixin
from braces.views import JSONResponseMixin
Quickstart
from django.views.generic import TemplateView
from braces.views import LoginRequiredMixin
class SomeSecretView(LoginRequiredMixin, TemplateView):
template_name = "path/to/secret_template.html"
login_url = "/login/" # Optional: Redirect URL if not authenticated
redirect_field_name = "next" # Optional: Parameter name for the next URL
raise_exception = False # Optional: Raise PermissionDenied instead of redirecting
def get(self, request, *args, **kwargs):
return self.render_to_response({})