Skip to content

Django

The batteries-included framework — ORM, admin, auth, migrations and templates in one coherent package.

Web & HTTPWebPython

What it is

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It includes built-in tools for database ORM, authentication, templating, routing, and an admin interface.

Django enables developers to quickly create web applications with structured models, views, templates, and URLs. It provides an admin interface out-of-the-box and includes features like authentication, sessions, forms, and security mechanisms.

Best known for
The auto-generated admin site, still unmatched in other ecosystems
Licence
BSD 3-clause
Watch for
Django REST Framework is the usual addition when building APIs

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • Content-driven or database-heavy applications with users, roles and permissions
  • The free admin interface would genuinely save you building a back office
  • A small team needs to ship a large application and wants decisions already made

Look elsewhere when

  • You only need a handful of JSON endpoints — the framework's weight will not pay for itself
  • Your workload is heavily async; Django's async support is real but less natural than FastAPI's

Installation

pip install django

Getting started

The smallest useful thing you can do with it, and what each part means.

Creating a new project
# Terminal commands
django-admin startproject mysite
cd mysite
python manage.py runserver
Starts a new Django project named 'mysite'. Running the server starts a local development server at http://127.0.0.1:8000/.
Creating a new app
# Terminal commands
python manage.py startapp blog
Creates a new Django app called 'blog' inside your project. Apps encapsulate related models, views, and templates.

Advanced usage

Where the library earns its place over a simpler alternative.

Defining models and migrations
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
Defines a Post model. Use `python manage.py makemigrations` and `python manage.py migrate` to create the database schema.
Views and URL routing
from django.shortcuts import render
from django.urls import path

# views.py
def home(request):
    return render(request, 'home.html')

# urls.py
urlpatterns = [path('', home, name='home')]
Defines a view function `home` and maps it to the root URL. Django uses URLconf to connect URLs to views.
Using the Django Admin Interface
# Register model in admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Adds your model to the admin interface so you can manage database records through the built-in web UI.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

OperationalError
Ensure migrations are applied and database is accessible.
TemplateDoesNotExist
Check the template directory settings and file paths.

Best practices

  • Keep apps modular and reusable.
  • Use Django's built-in authentication and permission system for security.
  • Leverage class-based views for complex logic.
  • Separate settings for development and production.
  • Use environment variables for sensitive data.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

Django was created by Adrian Holovaty and Simon Willison in 2005 at the Lawrence Journal-World newspaper. Its goal was to simplify web development by providing reusable components and a 'batteries-included' philosophy. Over time, it became one of the most widely used Python frameworks for building scalable, secure web applications.