django

Django custom error pages (404, 500)

Custom error pages improve user experience and brand consistency. I create templates named 404.html, 500.html, 403.html, and 400.html in the templates root. Django serves these automatically when DEBUG=False. For custom logic, I can override error han

Django admin customization with ModelAdmin

The Django admin is powerful when customized. I set list_display for column layout, list_filter and search_fields for finding records. Using readonly_fields, I prevent editing certain fields. The fieldsets organize the form layout. For computed values

Django custom template filters for formatting

Custom template filters transform variables in templates. I decorate functions with @register.filter and optionally set is_safe=True for HTML output. Filters take one or two arguments. For formatting dates, numbers, or text, filters keep logic out of

Django custom middleware for request/response modification

Custom middleware intercepts all requests and responses. I implement __init__ and __call__ methods. Middleware can modify requests before views, modify responses after views, or short-circuit entirely. Common uses include authentication, logging, CORS

Django multi-tenancy with django-tenant-schemas

Multi-tenancy allows multiple clients to share one application with isolated data. I use django-tenant-schemas for PostgreSQL schema-based isolation. Each tenant gets a separate schema (database namespace). The middleware routes requests to correct te

Django Q objects for complex queries

Q objects enable complex query logic with OR, AND, and NOT operations. I combine them with | for OR and & for AND. The ~Q() syntax negates a condition. This is cleaner than raw SQL for dynamic filters. I build Q objects conditionally based on user

Django transaction handling with atomic decorator

Database transactions ensure data integrity when multiple operations must succeed or fail together. I use @transaction.atomic on views or functions to wrap them in a transaction. For partial rollbacks, I use transaction.atomic() as a context manager a

Django REST Framework viewset with custom permissions

I create custom permission classes to encapsulate authorization logic outside of views. This IsOwnerOrReadOnly pattern is useful for resources where anyone can read but only the owner can modify. By implementing has_object_permission, I can make granu

Django custom management command for data import

Management commands are perfect for scripts that need Django's ORM and settings. I create them in management/commands/ and extend BaseCommand. The add_arguments method defines CLI options using argparse. I use self.stdout.write with style helpers for

Django custom authentication backend

Custom auth backends enable alternative authentication methods. I subclass ModelBackend and override authenticate(). Common use cases include email login, LDAP, OAuth, or custom token auth. The backend returns a user object or None. I add it to AUTHEN

Django CORS configuration for API access

Cross-Origin Resource Sharing (CORS) enables frontend apps on different domains to access your API. I use django-cors-headers for production-ready CORS handling. I configure CORS_ALLOWED_ORIGINS for specific domains in production and use CORS_ALLOW_AL

Django custom template tags for reusable components

Template tags extend Django's template language. I create simple tags with @register.simple_tag and inclusion tags with @register.inclusion_tag for rendering template snippets with context. Assignment tags store results in template variables. I place