Learn Django

Django is a high-level Python web framework that encourages rapid, clean, and pragmatic development of database-backed websites.

What Is Django?

Django is a free, open-source web framework written in Python that follows a 'batteries-included' philosophy. Rather than forcing you to assemble a stack from dozens of small libraries, Django ships with an ORM, an admin interface, a templating engine, a forms library, authentication, and a security layer built in. It was extracted from a newsroom content-management system in 2003 and has since become one of the most widely used Python web frameworks in production, powering sites like Instagram, Pinterest, Mozilla, and Disqus.

The MTV Pattern

Django organizes code using a variation of the classic Model-View-Controller pattern called MTV: Model, Template, View. The names differ from MVC, but the separation of concerns is similar. Django itself plays the role of the controller, routing each incoming request to the correct Python function based on the requested URL.

  • Model - defines the shape of your data and talks to the database through Django's ORM
  • Template - the HTML (plus a small templating language) that defines how data is displayed
  • View - a Python function or class that receives a request, works with the Model, and returns a Template or other response
MVC TermDjango (MTV) Term
ModelModel
ViewTemplate
ControllerView

Example 1: A Minimal View Function

# views.py
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django!")

Example 2: Wiring the View to a URL

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="home"),
]
Note: In classic MVC, the piece that handles a request is called a 'controller'. In Django's MTV naming, that same job is done by a 'view' - keep this mapping in mind so the terminology doesn't trip you up later.

Why Developers Choose Django

Django's ORM lets you model data as Python classes instead of writing raw SQL, its automatic admin site gives you a working back office for free, and its security defaults guard against common vulnerabilities like SQL injection, cross-site scripting, and clickjacking out of the box. Because the framework scales from a weekend prototype to sites serving hundreds of millions of users, teams rarely outgrow it.

Example 3: Registering an App in settings.py

# settings.py
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "pages",  # our own app
]

Exercise: Django Introduction

What type of framework is Django primarily described as?

Frequently Asked Questions

Should I learn Django or Flask first?
Django if you want to build a complete site quickly: authentication, admin, database layer and forms all ship with it. Flask if you want to understand each piece by assembling it yourself. Django's conventions save time; Flask's minimalism teaches more about the parts.
How much Python do I need before Django?
Comfortable with functions, classes, modules and packages. Django leans heavily on classes and inheritance, so object-oriented basics are not optional. You do not need advanced topics like decorators or async to start, though both appear later.
What is the Django admin actually for?
It generates a working database management interface from your models, so staff can create and edit records on day one without anyone building screens for it. It is meant for internal use by trusted users, not as a customer-facing interface.