or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

access-control.mdajax-json.mdform-processing.mdhttp-utilities.mdindex.mdquery-optimization.md
tile.json

tessl/pypi-django-braces

Reusable, generic mixins for Django class-based views

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/django-braces@1.17.x

To install, run

npx @tessl/cli install tessl/pypi-django-braces@1.17.0

index.mddocs/

Django Braces

Django Braces provides a comprehensive collection of reusable mixins for Django's class-based views. Most mixins replicate the behavior of Django's function-based view decorators, while others solve common headaches with class-based views. The library offers 24 view mixins and 1 form mixin organized into functional categories for authentication, AJAX handling, form processing, caching, and query optimization.

Package Information

  • Package Name: django-braces
  • Language: Python
  • Installation: pip install django-braces
  • Django Compatibility: 2.2-4.2
  • Python Compatibility: 3.6-3.13

Core Imports

from braces.views import LoginRequiredMixin, JSONResponseMixin, MessageMixin

For form mixins:

from braces.forms import UserKwargModelFormMixin

Basic Usage

from django.views.generic import ListView, CreateView
from braces.views import LoginRequiredMixin, JSONResponseMixin, MessageMixin

# Simple authentication requirement
class ProtectedListView(LoginRequiredMixin, ListView):
    model = MyModel
    login_url = '/login/'

# AJAX-enabled view with JSON responses
class AjaxDataView(JSONResponseMixin, ListView):
    model = MyModel
    
    def get(self, request, *args, **kwargs):
        if request.headers.get('x-requested-with') == 'XMLHttpRequest':
            data = list(self.get_queryset().values('id', 'name'))
            return self.render_json_response({'results': data})
        return super().get(request, *args, **kwargs)

# Form with success messages
class CreateWithMessageView(MessageMixin, CreateView):
    model = MyModel
    
    def form_valid(self, form):
        response = super().form_valid(form)
        self.messages.success('Item created successfully!')
        return response

Architecture

Django Braces uses Python's multiple inheritance to add functionality to Django's class-based views. Each mixin focuses on a specific concern and can be combined with other mixins and Django's generic views:

  • Access Mixins: Authentication and authorization controls
  • AJAX Mixins: AJAX request handling and JSON responses
  • Form Mixins: Form processing enhancements and messaging
  • Utility Mixins: Context manipulation and HTTP header management
  • Query Mixins: Database query optimization helpers

Mixins are designed to be combined safely, with clear inheritance order requirements documented for each mixin.

Capabilities

Access Control and Authentication

Authentication requirements, permission checking, group membership validation, and secure connection enforcement. These mixins provide the building blocks for securing Django class-based views.

class LoginRequiredMixin:
    login_url = None
    raise_exception = False
    redirect_field_name = 'next'
    
class PermissionRequiredMixin:
    permission_required = None
    object_level_permissions = False
    
class GroupRequiredMixin:
    group_required = None

Access Control

AJAX and JSON Response Handling

AJAX request detection, JSON response generation, and request body parsing. These mixins enable seamless AJAX integration with class-based views.

class JSONResponseMixin:
    content_type = None
    json_dumps_kwargs = None
    json_encoder_class = None
    
class AjaxResponseMixin:
    def get_ajax(self, request, *args, **kwargs): ...
    def post_ajax(self, request, *args, **kwargs): ...

AJAX and JSON

Form Processing and Messaging

Form validation messages, CSRF exemption, user context injection, and success URL handling. These mixins enhance Django's form processing capabilities.

class MessageMixin:
    messages = _MessageDescriptor()
    
class FormValidMessageMixin:
    form_valid_message = None
    def get_form_valid_message(self): ...
    
class UserFormKwargsMixin:
    def get_form_kwargs(self): ...

Form Processing

HTTP and Caching Utilities

HTTP header manipulation, cache control, context enhancement, and URL canonicalization. These mixins provide essential web development utilities.

class HeaderMixin:
    headers = {}
    def get_headers(self, request): ...
    
class CacheControlMixin:
    cachecontrol_max_age = None
    cachecontrol_public = None
    cachecontrol_private = None
    cachecontrol_no_cache = None
    cachecontrol_must_revalidate = None

HTTP Utilities

Database Query Optimization

Query optimization through select_related, prefetch_related, and dynamic ordering. These mixins improve database performance in list views.

class SelectRelatedMixin:
    select_related = None
    def get_queryset(self): ...
    
class OrderableListMixin:
    orderable_columns = None
    orderable_columns_default = None
    ordering_default = None
    order_by = None
    ordering = None

Query Optimization

Form Enhancement

Form validation helpers and user context injection for enhanced form handling in class-based views.

class UserKwargModelFormMixin:
    user = None
    
    def __init__(self, *args, **kwargs):
        """Remove user from kwargs and assign to instance"""

This form mixin is available directly from braces.forms and is documented within the Form Processing section above.