Skip to content

Django: The 80-20 Guide#

A practical guide to building production-ready Django applications. This guide covers the 20% of Django that you'll use 80% of the time.

🎯 What You'll Learn#

  • Project Setup - Create projects, apps, and configure settings
  • Views & URLs - Handle requests and route them properly
  • Templates - Build dynamic HTML with Django template language
  • Models - Define database schema with relationships
  • Migrations - Manage database schema changes
  • ORM Queries - Efficient database queries (80-20 patterns)
  • CRUD Operations - Create, Read, Update, Delete patterns
  • Admin Interface - Customize Django admin (80-20 features)
  • Forms - Handle form validation and processing
  • Static & Media Files - Serve static assets and user uploads
  • Production Settings - Configure Django for production
  • Advanced Topics - Transactions, caching, debug toolbar
  • Authentication Flow - Complete user authentication system
  • OAuth with Google - Social authentication integration
  • Django Channels - WebSocket support for real-time features
  • Signals - Event-driven programming with Django signals
  • Celery & Celery Beat - Background tasks and periodic scheduling

📚 Sections#

1. Getting Started#

Project setup, creating apps, and basic configuration.

2. Views, URLs & Templates#

Handle requests, route URLs, and render templates with forms and template tags.

3. Models#

Define database models with all field types and relationships.

4. Database & Migrations#

Configure databases (SQLite, PostgreSQL, MySQL), create migrations, and run raw SQL.

5. ORM Queries#

80-20 ORM patterns: filter, Q objects, sorting, select_related, aggregate, annotate, and more.

6. CRUD Operations#

Complete Create, Read, Update, Delete patterns for Django views.

7. Admin Customization#

Customize Django admin interface with 80-20 features and format_html.

8. Forms#

Django Forms and ModelForms for validation and data handling.

9. Static & Media Files#

Configure static files and media uploads in settings.

10. Settings & Production#

Production-ready settings.py configuration and best practices.

11. Advanced Topics#

Transactions, caching, Django Debug Toolbar, generic views, and more.

12. Authentication Flow#

Complete user authentication: registration, login, logout, password reset.

13. OAuth with Google#

Integrate Google OAuth authentication using django-allauth.

14. Django Channels#

WebSocket support for real-time features like chat and notifications.

15. Signals#

Event-driven programming with Django signals (model, user, custom signals).

16. Celery & Celery Beat#

Background tasks, periodic scheduling, and async task processing.

🚀 Quick Start#

# Install Django
pip install django

# Create project
django-admin startproject myproject
cd myproject

# Create app
python manage.py startapp myapp

# Run migrations
python manage.py migrate

# Create superuser
python manage.py createsuperuser

# Run server
python manage.py runserver

Visit http://localhost:8000

📁 Project Structure#

myproject/
├── manage.py
├── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
│   └── asgi.py
├── myapp/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── views.py
│   ├── urls.py
│   ├── forms.py
│   ├── migrations/
│   └── templates/
│       └── myapp/
└── static/
└── media/

🎓 Core Concepts#

MVT Pattern#

Model - Database schema and business logic

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()

View - Request handler

def post_list(request):
    posts = Post.objects.all()
    return render(request, 'posts/list.html', {'posts': posts})

Template - HTML with Django template language

{% for post in posts %}
    <h2>{{ post.title }}</h2>
    <p>{{ post.content }}</p>
{% endfor %}

Request Flow#

  1. URL → Routes request to view
  2. View → Processes request, queries database
  3. Template → Renders HTML response
  4. Response → Returns to client

Apps vs Projects#

  • Project - Entire Django application (created with startproject)
  • App - Reusable component within project (created with startapp)

⚡ Key Features#

Built-in Admin#

Django provides a powerful admin interface out of the box:

# admin.py
from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ['title', 'created_at']
    search_fields = ['title']

ORM (Object-Relational Mapping)#

Query database using Python instead of SQL:

# Get all posts
posts = Post.objects.all()

# Filter posts
recent = Post.objects.filter(created_at__gte=timezone.now() - timedelta(days=7))

# Get single object
post = Post.objects.get(id=1)

Migrations#

Version control for your database schema:

# Create migrations
python manage.py makemigrations

# Apply migrations
python manage.py migrate

# Show migration status
python manage.py showmigrations

📖 Best Practices#

  1. One app per feature - Keep apps focused and reusable
  2. Use migrations - Never edit database directly
  3. Environment variables - Use python-decouple or django-environ
  4. Type hints - Use Python type hints for better IDE support
  5. Class-based views - Prefer CBVs for complex views
  6. Model methods - Put business logic in models
  7. Template inheritance - Use base templates to avoid repetition

🛠️ Essential Packages#

# Environment variables
pip install python-decouple

# Database
pip install psycopg2-binary  # PostgreSQL
pip install mysqlclient      # MySQL

# Development
pip install django-debug-toolbar
pip install django-extensions

# Production
pip install gunicorn
pip install whitenoise

Next Steps#

Start with Getting Started to set up your Django project, then follow through each section to build a complete application.


Pro Tip: Django follows the "batteries included" philosophy - most features you need are already built-in. Learn the ORM well, it's Django's superpower!