Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 77 ยท Getting Started

Django Introduction

Meet Django, the batteries-included Python web framework: the MTV pattern, the request and response cycle, what Django 5 and 6 add, and a first live view.

Django is a web framework for Python. It gives you the parts almost every website needs: a way to map URLs to code, a database layer, a template language, forms, user accounts, an admin interface and security protections. You write your own logic on top, and Django handles the plumbing.

The framework began in a newsroom in the mid-2000s, where deadlines were tight and every site needed the same features. That history explains its motto: it is meant for "perfectionists with deadlines". Today Django powers everything from small internal tools to very large sites, and it is one of the most popular ways to build a backend in Python.

This lesson is the map for the whole tutorial. You will see what "batteries included" really means, how Django organizes a project (the MTV pattern), what happens between a click and a rendered page, and which newer features are worth knowing. Along the way you will run real Django code in your browser.

What Django is for

Django is a full-stack framework. That means one package covers the server side from end to end:

  • Routing: match a URL such as /books/42/ to a Python function.
  • ORM: read and write database rows using Python classes instead of hand-written SQL.
  • Templates: build HTML pages from data, with inheritance and automatic escaping.
  • Forms: validate user input and show helpful errors.
  • Authentication: users, passwords, sessions, permissions and groups.
  • Admin: a ready-made back office generated from your models.
  • Security: protection against CSRF, XSS, SQL injection and clickjacking by default.
  • Extras: caching, email, signals, internationalization, a test framework and more.

Some frameworks, like Flask, start tiny and let you add pieces. Django makes the opposite trade: everything is already there and designed to work together, so you spend your time on your product rather than on choosing and wiring libraries. The price is that Django has opinions about how a project should look. In return, any Django developer can open your project and find their way around.

Batteries included, in code

The phrase "batteries included" is easy to say and easier to believe once you see it. The next example uses only things that ship with Django: signed values, a validator, text and HTML helpers and a paginator. (Password hashing, user accounts and the admin are also built in; you will meet them in later lessons.) None of them needs a second library.

Python A tour of built-in batteries

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what the first lines do. Django normally reads a settings file, but here settings.configure(...) supplies the settings in code, then django.setup() loads the framework. You will use this pattern throughout the tutorial because a browser tab cannot create project folders. On your own computer you will use real project files instead (next lesson).

The live examples install Django from the Python package index the first time you press Run, so the first run takes a few seconds. They run entirely in your browser: there is no web server, no manage.py and no files on disk. Where something genuinely needs a server, a terminal or Docker, the lesson shows it as a read-only snippet and explains it.

The MTV pattern

You may have heard of MVC (model, view, controller). Django has the same three jobs but names them differently, which confuses beginners at first:

Django part Job Roughly equals in MVC
Model Describes your data and talks to the database Model
Template Turns data into HTML (presentation) View
View A Python function or class that handles a request and decides what to return Controller

The framework itself acts as the controller that routes each request to the right view. So the word "view" in Django means "the code that responds to a request", not "the thing that is drawn on screen". Keep that in mind and the naming stops being confusing.

flowchart LR
    B["Browser"] -->|"request"| U["URL configuration"]
    U --> V["View"]
    V <--> M["Model<br/>(database)"]
    V --> T["Template"]
    T -->|"HTML"| V
    V -->|"response"| B

A view receives the request, asks the model for data, hands it to a template and returns the response.

The three pieces working together

Here is the whole pattern in one small program: a Book model with an in-memory SQLite database, a template that lists books, and a view that connects them. A test client plays the role of the browser.

Python Model, template and view

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the output and then the code once more. Book.objects.all() returned the books ordered by year because of Meta.ordering. The template looped over them and the view returned an HttpResponse. The project module is a stand-in for the urls.py file a real project has; Django only needs a module that defines urlpatterns.

The request and response cycle

Every Django page follows the same path, which a later lesson explores in detail:

  1. A web server (Gunicorn, Uvicorn, or the development server) receives the HTTP request and hands it to Django through WSGI or ASGI.
  2. Django builds an HttpRequest object and passes it through middleware.
  3. The URL resolver matches the path against urlpatterns and picks a view.
  4. The view runs: it may query models, validate a form and render a template.
  5. The view returns an HttpResponse, which travels back out through the middleware to the browser.

Each of those steps is a place where you can add or change behavior, and each one has its own lesson later in the tutorial.

What recent Django versions add

Django ships a feature release roughly every eight months, and every third release (such as 5.2) is a long-term support (LTS) version that gets security fixes for years. Highlights of the 5.x and 6.x series that you will meet in this tutorial:

  • Django 5.0: db_default (database-level default values), GeneratedField (columns computed by the database), simpler form rendering with field groups, and async improvements for authentication and signals.
  • Django 5.1: a built-in LoginRequiredMiddleware, a {% querystring %} template tag for pagination and filter links, and more async ORM support.
  • Django 5.2 (LTS): composite primary keys and a friendlier shell that auto-imports your models.
  • Django 6.0 and later: a built-in framework for background tasks, template partials, and first-class support for Content Security Policy headers.

Don't memorize this list. The point is that Django keeps modernizing while keeping upgrades predictable: deprecated features warn for two releases before they disappear. Read the release notes before you upgrade a real project, and pin your version in requirements.txt.

What runs here and what runs on your machine

This site runs Python in your browser, which is perfect for learning the ORM, templates, forms and views, because all of them work in-process with a test client. Three things need your own computer:

  • manage.py runserver, a long-running development server that you open in a real browser.
  • Creating project files with startproject and startapp.
  • Real services such as PostgreSQL, Redis, Celery workers and Docker, used mainly in the deployment lessons.

A good learning path is to read each lesson here, then rebuild the same idea in a real project folder on your computer. The next lesson takes you through exactly that setup.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Django's MTV pattern, which part is closest to the 'controller' in MVC?

    A Django view receives the request, works with models and templates and returns a response, which is the controller's job. Templates handle presentation.
  2. 2. Why does Django describe itself as 'batteries included'?

    Most things a typical website needs are part of Django itself and are designed to work together, so you add third-party packages only when you need something special.
  3. 3. What does settings.configure(...) followed by django.setup() let you do?

    It supplies settings in code and loads the framework, which is how the live examples in this tutorial run Django inside a browser tab.
  4. 4. Where does the HTTP request first enter a Django application?

    The web server passes the request to Django via WSGI or ASGI. Middleware wraps the request, then the URL resolver chooses the view.

Summary

  • Django is a batteries-included Python web framework: routing, ORM, templates, forms, auth, admin and security come in one package.
  • MTV stands for Model (data), Template (presentation) and View (request-handling logic); Django's URL system acts as the controller.
  • A request travels through WSGI or ASGI, middleware, the URL resolver and a view, and the response travels back.
  • settings.configure() plus django.setup() lets you run Django in-process, which powers the live examples here.
  • Recent versions add db_default, GeneratedField, LoginRequiredMiddleware, composite primary keys, template partials and background tasks.
  • Dev servers, project scaffolding and external services run on your own machine.

Next lesson: Installing Django and Your First Project.