Backend

Django

A batteries-included Python framework: ORM, admin, auth and templates.

What it is

Django gives everything for a web app out of the box: an ORM with migrations, an automatic admin panel, users and permissions, forms, templates, CSRF and XSS protection.

The architecture is MTV (model, template, view). Models describe tables, URL config maps addresses to views, and Django REST Framework adds APIs.

The framework has strict conventions, so projects from different teams look alike. Modern versions support async views and ASGI.

Use it when

  • Content sites, CRMs and internal tools that need an admin right away.
  • The team writes Python and wants a proven stack.
  • A fast MVP with a database, auth and an API.

Avoid it when

  • A tiny microservice with a few endpoints: FastAPI or Flask is lighter.
  • A very unusual data model that fits the Django ORM poorly.

Key concepts

ORM і міграції
Python models generate SQL and schema changes.
Admin
An automatic CRUD interface built from models.
Views / URLconf
Functions or classes that handle requests, and the route table.
Middleware
Request processing layers: sessions, CSRF, auth.

Example

A model and a view returning published articles.

Cheat sheet

Django cheat sheet
Command or codeWhat it does
django-admin startproject siteCreates a project
python manage.py startapp blogCreates an app
python manage.py runserverDev server
python manage.py makemigrationsCreates migrations from model changes
python manage.py migrateApplies migrations
python manage.py createsuperuserAdmin user
Post.objects.filter(published=True)Database query through the ORM
Post.objects.select_related('author')Fewer queries for relations
path('posts/<int:pk>/', views.detail)URL route
python manage.py testRuns tests
python manage.py shellInteractive project shell

How to learn it

  1. First take the Python course on ShortLesson.
  2. Create a project and an app with django-admin and manage.py.
  3. Learn URL routes, views and templates.
  4. Define models and run migrations.
  5. Enable the Django admin and configure how models are shown.
  6. Add forms with validation and authentication.
  7. Build an API with Django REST Framework.
  8. Write tests and prepare the project for deployment with PostgreSQL.

Interview questions

  1. 1. What is the N+1 problem in the Django ORM and how do you fix it?

    It is when a separate query is run for related data for each item in a list. Fix it with select_related for foreign keys and prefetch_related for many relations.

  2. 2. How do migrations work?

    makemigrations compares models with the current state and writes a change file, migrate applies it to the database. Migration files are committed to the repository.

  3. 3. What is Django REST Framework for?

    It adds serializers, viewsets, authentication and pagination for building REST APIs on top of Django.

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).