TExcelTAD
Home › Django › Django Intro

Django Intro

Django is a free, open-source web framework written in Python. It gives you everything a typical database-backed website needs — URL routing, HTML templates, an ORM for talking to the database, forms, user accounts, an admin site and strong security defaults — in one coherent package. Its motto is "the web framework for perfectionists with deadlines".

Django was created in 2003 at a newspaper in Lawrence, Kansas, where developers had to ship features on news-room deadlines. It was released as open source in 2005 and is now maintained by the Django Software Foundation. Sites such as Instagram, Mozilla, Disqus and many government portals have been built on it. This course uses Django 5.2, a long-term-support (LTS) release.

Why Django?

Feature What it means for you
Batteries included Auth, sessions, admin, forms, caching, i18n and more ship with Django — no hunting for plugins
ORM Describe tables as Python classes and query them with Python instead of hand-written SQL
Admin site A ready-made back office to add, edit and delete data, generated from your models
Security Protection against SQL injection, XSS, CSRF and clickjacking is on by default
Scales The same code runs a hobby blog or a site with millions of users
Documentation Django's official docs are famously thorough

The MVT pattern

Most web frameworks follow some flavour of Model–View–Controller. Django calls its version Model–View–Template (MVT):

  • Model — a Python class that describes a database table (fields, types, relationships). The ORM turns model operations into SQL.
  • View — a Python function (or class) that receives an HTTP request and returns an HTTP response. It decides what data to show.
  • Template — an HTML file with placeholders ({{ name }}) and tags ({% for %}) that decides how the data looks.

On top of that, a URLconf (urls.py) maps URL paths to views. A request flows like this:

text
Browser ──GET /books/──▶ URLconf ──▶ view ──▶ Model (database)
                                       │
                                       ▼
Browser ◀──HTML response── Template ◀─┘

A taste of Django

Below is a complete, working Django program. It configures Django, defines one view, maps it to a URL and sends a fake request with Django's test client. Press Run — the playground installs real Django in your browser the first time (this takes a few seconds).

python · try it yourself
import sys, types, django
from django.conf import settings
sys.modules["urls"] = urls = types.ModuleType("urls")
settings.configure(SECRET_KEY="demo", ROOT_URLCONF="urls", ALLOWED_HOSTS=["testserver"])
django.setup()

from django.http import HttpResponse
from django.urls import path
from django.test import Client

def hello(request):
    return HttpResponse("Hello from Django " + django.get_version())

urls.urlpatterns = [path("hello/", hello)]

response = Client().get("/hello/")
print(response.status_code)
print(response.content.decode())
200
Hello from Django 5.2.18

Don't worry about the first five lines yet — they replace the settings file a real project would have. The lesson How the Playground Runs Django explains them in detail.

Models in one glance

The ORM is Django's other superpower. Here a Book class becomes a table, two rows are inserted and then queried — without a single line of SQL:

python · try it yourself
import django
from django.conf import settings
settings.configure(DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}})
django.setup()
from django.db import models, connection

class Book(models.Model):
    title = models.CharField(max_length=100)
    year = models.IntegerField()

    class Meta:
        app_label = "demo"

with connection.schema_editor() as editor:
    editor.create_model(Book)

Book.objects.create(title="Dune", year=1965)
Book.objects.create(title="Neuromancer", year=1984)

for book in Book.objects.filter(year__gt=1970):
    print(book.id, book.title, book.year)
print("Total books:", Book.objects.count())
2 Neuromancer 1984
Total books: 2

What you need to know first

Django is Python, so you should be comfortable with Python basics: variables, functions, lists, dictionaries, classes and modules. Some HTML knowledge helps for the template lessons. You do not need to know SQL, although it will help you understand what the ORM does.

Tip: Every runnable example in this course is a self-contained Django program. Change it, run it again, break it — nothing is saved and every run starts with a fresh, empty database.

Test yourself

What does the "T" in Django's MVT pattern stand for?

  1. Table
  2. Template
  3. Transaction
  4. Test

In Django, which component receives the HTTP request and returns a response?

  1. The model
  2. The template
  3. The view
  4. The migration

What does Django's ORM let you do?

  1. Write CSS in Python
  2. Query the database using Python classes and methods
  3. Run JavaScript on the server
  4. Deploy to the cloud automatically