Intro to Django
Django is a high-level, "batteries included" Python web framework for building database-backed web applications quickly. Where a micro-framework like Flask gives you a small core and lets you assemble the rest yourself, Django ships with an ORM, an admin interface, authentication, form handling, templating, and a security layer already built in, so a large class of common web-application problems are solved before you write a single line of your own code.
The core pieces of Django
Django follows an MVT (Model-View-Template) architecture, a close cousin of the more widely known MVC pattern. Each request that hits a Django app flows through a predictable path: a URL pattern matches, a view function (or class) runs, it typically talks to models backed by the database, and finally renders a template (or returns JSON, for an API).
Component | Role |
|---|---|
Models | Python classes that define your data — Django’s ORM maps each model to a database table. |
Views | Functions or classes that receive a request, run logic, and return a response. |
Templates | HTML files with a templating language for rendering dynamic content. |
URLs (urls.py) | Maps URL patterns to the view that should handle them. |
Admin | Auto-generated, ready-to-use web UI for managing your models’ data. |
Forms | Handles rendering, validation, and cleaning of user-submitted data. |
Middleware | Hooks that process every request/response globally (auth, security headers, sessions). |
Creating a Django project
Django is installed like any other package, and it comes with a command-line tool, django-admin, for scaffolding new projects.
pip install django django-admin startproject mysite cd mysite python manage.py runserver
startproject generates a project skeleton: a manage.py script you'll use for almost every administrative command, and a package (also named mysite by default) holding project-wide settings, URL configuration, and the WSGI/ASGI entry points.
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.pyA Django project is organized into "apps" — self-contained modules that each handle one area of functionality (e.g. blog, accounts, orders). You create one with another management command:
python manage.py startapp blog
This creates a blog/ directory with its own models.py, views.py, admin.py, and migrations/ folder, which you then register in the project's INSTALLED_APPS setting.
Models and the ORM
Instead of writing raw SQL, you describe your data as Python classes. Django's ORM (Object-Relational Mapper) translates that into database tables and query operations.
# blog/models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
published = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.titleAfter defining or changing a model, you generate and apply a migration — a versioned, reviewable script describing the schema change:
python manage.py makemigrations python manage.py migrate
Querying reads like Python, not SQL:
from blog.models import Post
# All published posts, newest first
Post.objects.filter(published=True).order_by('-created_at')
# A single post by primary key
Post.objects.get(pk=1)
# Create and save a new post
Post.objects.create(title='Hello, Django', body='My first post.', published=True)The admin panel — Django's signature feature
Perhaps Django's most celebrated feature is the automatic admin interface. Register a model, and Django generates a full CRUD (create, read, update, delete) web UI for it — no HTML or views required.
# blog/admin.py from django.contrib import admin from .models import Post admin.site.register(Post)
python manage.py createsuperuser python manage.py runserver
Visiting /admin/ and logging in with the superuser account gives you a searchable, sortable, permission-aware interface for managing every registered model — genuinely useful for internal tools, content management, and rapid prototyping, not just a toy feature.
Views and URLs
# blog/views.py
from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.objects.filter(published=True)
return render(request, 'blog/post_list.html', {'posts': posts})# blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.post_list, name='post_list'),
]Flask vs Django
Flask and Django are the two most widely used Python web frameworks, but they sit at opposite ends of the "how much do you want built in" spectrum.
Aspect | Flask | Django |
|---|---|---|
Philosophy | Minimal core, add what you need | Batteries included, most things built in |
ORM | Not included (commonly SQLAlchemy) | Built-in ORM |
Admin panel | None built in | Full auto-generated admin UI |
Learning curve | Gentler to start, more decisions later | Steeper upfront, more conventions to learn |
Project structure | Flexible, largely up to you | Opinionated, enforced by tooling ( |
Best fit | Small services, APIs, microservices, full control | Content-heavy sites, admin-heavy apps, larger teams |
Django trades some flexibility for a huge amount of productivity: models, an ORM, an admin panel, forms, and authentication all come ready to use, letting you focus on your application's actual logic instead of re-building infrastructure every project needs.