Labs ICT
โญ Pro Login

Django Best Practices

Project organization, settings splitting, and security.

Project Organization

Start your Django project with a clear structure from day one. Group related apps together, keep settings in a dedicated directory, and separate configuration from code. A well-organized project is easier to maintain as it grows.

A common pattern is to have a config/ directory for settings, URLs, and WSGI/ASGI configs. Your apps live in an apps/ directory. This keeps the project root clean and makes it obvious where everything lives.

myproject/
    config/
        settings/
            base.py
            production.py
        urls.py
        wsgi.py
    apps/
        accounts/
        blog/
        api/
    templates/
    static/
    manage.py

Custom User Model

Always create a custom user model at the start of a new project, even if you don't need custom fields yet. Django's auth system makes it easy to extend, but switching later is painful.

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    bio = models.TextField(blank=True)
    avatar = models.ImageField(upload_to='avatars/', blank=True)

    def __str__(self):
        return self.username

Set AUTH_USER_MODEL = 'accounts.User' in your settings before running any migrations. This tells Django to use your custom model instead of the default one.

Try it Yourself โ†’

Settings Splitting

Don't put everything in one settings.py. Split it into base settings (shared across environments) and environment-specific files (development, production, testing).

from .base import *

DEBUG = False
ALLOWED_HOSTS = ['yourdomain.com']

STATIC_ROOT = '/var/www/static/'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('DB_NAME'),
    }
}

Use environment variables for secrets and settings that change between deployments. The python-decouple or django-environ packages make this easy and clean.

Security Essentials

Django has solid security defaults, but you should still be aware of the essentials. Use django.middleware.csrf.CsrfViewMiddleware (it's on by default), always validate and sanitize user input, and use Django's ORM to prevent SQL injection.

Set secure cookies with SESSION_COOKIE_SECURE = True and CSRF_COOKIE_SECURE = True in production. Use django-csp for Content Security Policy headers. Keep your dependencies updated โ€” security vulnerabilities are found in packages regularly.

Caching Strategy

Caching can dramatically improve performance. Django supports cache at multiple levels: per-view, template fragment, low-level cache API, and even full-page cache.

from django.core.cache import cache

def get_popular_posts():
    posts = cache.get('popular_posts')
    if posts is None:
        posts = Post.objects.filter(published=True).order_by('-views')[:10]
        cache.set('popular_posts', posts, timeout=300)
    return posts

Start caching the expensive queries and computed values. Redis is the go-to cache backend โ€” it's fast, reliable, and supports advanced caching patterns. Don't cache everything blindly; cache what's slow and what's accessed often.

๐Ÿงช Quick Quiz

What is the best practice for Django settings in production?