TodayTools

Django flashcards

The Python web framework with everything built in.

Donate
Beginner friendly, starts from zero
Section
How to study
0
DjangoStart from zero · 1 of 12

What is Django, in one plain sentence?

Django is a free toolkit, written in the Python language, for building websites that remember things, such as a shop with accounts, orders and a staff back office.

A ready-made kitchen for cooking websites.

All topics

Django at a glance

Django is a Python toolkit for building websites with accounts, forms, data and an admin, all included.

Like a restaurant: the URL is the order, the view is the chef, the model is the store room and the template is the plate.

Words you will meet

Request
What the browser sends to ask the server for a page
View
A Python function that takes a request and returns a response
Model
A Python description of one database table
Template
An HTML page with blanks that the view fills in
Migration
A saved step that changes the database table layout
ORM
The translator that lets you use the database from Python
App
One feature of the site, such as billing, in its own folder

The steps

  1. 1. Start from zero (12 cards): What Django is, how a web page travels, and the words you need before Core basics.
  2. 2. Core basics (16 cards): Projects, apps, the request path, views, URLs, models, migrations, templates and the admin.
  3. 3. Next steps (16 cards): Relations, lazy queries, F and Q, forms, CSRF, class views, auth, tests and constraints.
  4. 4. Go deeper (17 cards): Query speed, transactions and locks, middleware, async, tasks, deployment and Django 6.
All 12 cards in Start from zero
What is Django, in one plain sentence?
Django is a free toolkit, written in the Python language, for building websites that remember things, such as a shop with accounts, orders and a staff back office.
Before toolkits like Django, what did building a website with logins mean?
You wrote everything yourself: sign-in pages, password safety, forms, saving data and protection from attacks. Each team rebuilt the same parts, and small mistakes became security holes.
Where do you already meet sites like the ones Django builds?
Every time you sign in to a website, fill in a form, or check your order history, code like Django is doing the work on the other side: checking you, saving data and building the page.
What is a good everyday picture of how Django works?
A restaurant. The URL is the customer's order, the view is the chef who handles it, the model is the store room of ingredients, and the template is how the dish is plated.
What are a browser, a server, a request and a response?
Your browser (the app you browse the web with) sends a request to a server, a computer that stays on, asking for a page at a URL (web address). The page that comes back is the response.
What are Python, a package, pip and the terminal?
Python is the programming language Django is written in. A package is shared code you can install; pip is the tool that installs it. The terminal is a window where you type commands like pip install django.
What is a database, and what are tables, rows and columns?
A database is where a website keeps its data safely. It stores data in tables, like sheets in a spreadsheet: each row is one thing (one order) and each column is one fact about it (its total).
What do model, ORM and migration mean?
A model is a Python description of one table. The ORM lets you read and save data in Python instead of SQL, the database's own language. A migration is a saved step that changes the table layout.
What do view, template and HTML mean?
HTML is the language web pages are written in. A template is an HTML page with blanks to fill in, and a view is a Python function that takes a request, gets data and returns the filled-in page.
Your first tiny win: what does this Django code do?
This is a view: a Python function that gets the request from the browser and returns a response with the words Hello, Karthik! as the page.
What do you need before you start?
Python 3.12 or newer, a text editor, a terminal and a browser. Django brings a small built-in database (SQLite) for practice, so there is nothing else to set up.
After this topic, what will you be able to do?
Start a Django project, connect URLs to views, store data with models, show it with templates, and give staff an admin back office to manage it.
Django cheatsheet

manage.py

  • runserver 0.0.0.0:8000 Dev server on all interfaces
  • makemigrations app --name add_sku Write a named migration
  • migrate app 0007 Migrate to (or back to) a version
  • sqlmigrate app 0007 Show the SQL of a migration
  • showmigrations Applied and pending migrations
  • shell / dbshell Python with models / raw database shell
  • test app --parallel --keepdb Fast test run
  • check --deploy Production settings audit

ORM

  • Model.objects.filter(...).exclude(...) WHERE / WHERE NOT
  • get_object_or_404(Model, pk=pk) One row or 404
  • .select_related("fk") / .prefetch_related("m2m") Kill N+1
  • .only("id", "name") / .defer("body") Load fewer columns
  • .values_list("id", flat=True) Plain list of ids
  • .update(stock=F("stock") - 1) Atomic SQL update
  • .annotate(n=Count("items")) Per-row aggregate
  • bulk_create(objs, batch_size=1000) Insert many fast
  • .select_for_update() Row lock inside atomic()

Lookups

  • __exact / __iexact Equal / case-insensitive
  • __contains / __icontains Substring
  • __in=[1, 2, 3] In a list
  • __gt / __gte / __lt / __lte Comparisons
  • __range=(start, end) Between, inclusive
  • __isnull=True IS NULL
  • customer__city Follow a relation
  • __date / __year Date parts (can skip indexes)

Templates

  • {% extends "base.html" %} / {% block %} Inheritance
  • {% url "orders:detail" order.pk %} Link by name
  • {% for x in xs %}...{% empty %} Loop with empty state
  • {{ value|date:"d M Y" }} Format a date
  • {{ total|floatformat:2 }} Two decimals
  • {% querystring page=2 %} Keep query, change one key (5.1+)
  • {% partialdef row %} / list.html#row Template partials (6.0+)
  • {% csrf_token %} CSRF field in forms

Production settings

  • DEBUG = False Never True in prod
  • ALLOWED_HOSTS = ["shop.example.in"] Accepted Host headers
  • SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") Trust the TLS proxy
  • SECURE_HSTS_SECONDS = 31536000 HSTS for a year
  • SESSION_COOKIE_SECURE = CSRF_COOKIE_SECURE = True HTTPS-only cookies
  • CSRF_TRUSTED_ORIGINS = ["https://shop.example.in"] Allowed POST origins
  • CONN_MAX_AGE = 60 Reuse DB connections
Donate