Django Braces

1.17.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This example demonstrates how to use the `LoginRequiredMixin` to restrict access to a Django class-based view. The mixin ensures that only authenticated users can access `SomeSecretView`, redirecting unauthenticated users to the specified `login_url`.

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({})

view raw JSON →