django

Django class-based view with mixins for reusability

Mixins let me compose view behavior without duplication. LoginRequiredMixin is essential for protecting views. I create custom mixins like UserOwnershipMixin to encapsulate common patterns. Mixin order matters due to Python's MRO—I put Django's mixins

Django REST Framework authentication with JWT

JWT tokens provide stateless authentication for APIs. I use djangorestframework-simplejwt for token generation and validation. The TokenObtainPairView issues access and refresh tokens on login. I configure token lifetimes in settings. For protected en

Django formsets for editing multiple objects

Formsets handle multiple forms on one page. I use modelformset_factory for editing existing objects and inlineformset_factory for related objects. The extra parameter controls empty forms shown. I validate formsets with formset.is_valid() and save wit

Django sitemap generation for SEO

Sitemaps help search engines discover and index pages. Django's sitemap framework generates XML sitemaps automatically. I create sitemap classes for each content type, defining items(), lastmod(), changefreq(), and priority(). For large sites, I use s

Django model soft delete pattern

Soft deletes mark records as deleted without removing them from the database. I add a deleted_at field and override the delete() method. A custom manager excludes soft-deleted records by default. I provide a hard_delete() method for permanent removal.

Django background tasks with Celery beat for scheduling

Celery Beat schedules periodic tasks like cron jobs. I define schedules in settings or use the Django database scheduler. Tasks run at specified intervals or cron expressions. I use @periodic_task decorator or configure in CELERY_BEAT_SCHEDULE. For dy

Django database index strategies

Indexes dramatically improve query performance. I add indexes via Meta.indexes or db_index=True on frequently-filtered fields. Compound indexes help queries filtering on multiple fields. For text search, I use GinIndex on PostgreSQL. I index foreign k

Django logging configuration for production

Proper logging is critical for production debugging. I configure loggers for different apps and libraries. I use file handlers for persistence and console handlers for development. Log rotation prevents disk fill-up. I set appropriate levels (DEBUG, I

Django middleware for request ID tracking

Adding unique request IDs helps trace logs across distributed systems. I generate a UUID for each request and attach it to both the request object and response headers. I also add it to the logging context so all log entries for that request include t

Django model meta options for database optimization

Model Meta class controls database behavior. I use ordering for default query ordering, indexes for database indexes, and unique_together for composite uniqueness constraints. The db_table option customizes table names. For large tables, managed=False

Django internationalization and localization

Django's i18n framework supports multiple languages. I mark strings for translation with gettext() or _(). The makemessages command extracts strings to .po files. Translators fill in translations, then compilemessages creates binary .mo files. I use {

Django custom decorators for view logic

Custom decorators encapsulate reusable view logic. I use functools.wraps to preserve function metadata. For class-based views, I use method_decorator. Common patterns include permission checks, rate limiting, or request validation. Decorators can modi