Django

Python batteries-included web framework - ORM, admin panel, auth, and security built-in from day one

TL;DR

One-liner: Django is Python’s batteries-included web framework - everything you need, ready out of the box.

Core Strengths:

  • Admin panel - get a full CMS for free
  • ORM - database operations without SQL
  • Security - CSRF, XSS, SQL injection protection built-in
  • Django 6.0 - template partials, background tasks, CSP support

Core Concepts

Concept 1: MTV Pattern

Django uses Model-Template-View (MTV), similar to MVC:

Model     → Database structure (Python classes)
Template  → HTML with Django template language
View      → Business logic, connects models to templates

Concept 2: ORM

Define your database schema as Python classes. Django handles the SQL.

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    published = models.DateTimeField(auto_now_add=True)

# Usage - no SQL needed
Article.objects.all()
Article.objects.filter(title__contains='Django')
Article.objects.create(title='Hello', content='World')

Concept 3: URL → View → Template

Request flows through URLs to views, which render templates:

# urls.py
urlpatterns = [path('articles/', views.article_list)]

# views.py
def article_list(request):
    articles = Article.objects.all()
    return render(request, 'articles.html', {'articles': articles})

Quick Start

Install and Create Project

pip install django
django-admin startproject mysite
cd mysite
python manage.py runserver

Project Structure

mysite/
├── mysite/
│   ├── settings.py    # Configuration
│   ├── urls.py        # URL routing
│   └── wsgi.py        # WSGI entry point
├── manage.py          # CLI tool
└── db.sqlite3         # Default database

Create an App

python manage.py startapp blog

Minimal Example

# blog/views.py
from django.http import JsonResponse

def hello(request):
    return JsonResponse({'message': 'Hello Django!'})

# mysite/urls.py
from django.urls import path
from blog import views

urlpatterns = [
    path('hello/', views.hello),
]

Run

python manage.py runserver
# Open http://localhost:8000/hello/

Gotchas

Don’t forget to add apps to INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    ...
    'blog',  # Your app
]

Migrations are required after model changes

# After changing models.py
python manage.py makemigrations  # Create migration file
python manage.py migrate         # Apply to database

CSRF token required for POST forms

<form method="POST">
    {% csrf_token %}
    <input type="text" name="title">
    <button type="submit">Submit</button>
</form>

Static files need configuration

# settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']

# template
{% load static %}
<img src="{% static 'logo.png' %}">

When to Use

Best for:

  • Content-heavy websites (news, blogs)
  • Admin-heavy applications
  • Rapid prototyping with Python
  • Teams wanting convention over configuration

Not ideal for:

  • Microservices (use FastAPI)
  • Real-time applications (use FastAPI + WebSockets)
  • Simple REST APIs (Django REST Framework adds complexity)

Comparison:

FeatureDjangoFastAPIFlask
Learning curveMediumEasyEasy
Built-in featuresManyFewMinimal
Admin panelYesNoNo
Async supportPartialFullLimited

Next Steps

Cheatsheet

CommandDescription
django-admin startproject nameCreate project
python manage.py startapp nameCreate app
python manage.py runserverStart dev server
python manage.py makemigrationsCreate migrations
python manage.py migrateApply migrations
python manage.py createsuperuserCreate admin user
python manage.py shellPython shell with Django
python manage.py testRun tests