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
Cheat sheet
| Command or code | What it does |
|---|---|
django-admin startproject site | Creates a project |
python manage.py startapp blog | Creates an app |
python manage.py runserver | Dev server |
python manage.py makemigrations | Creates migrations from model changes |
python manage.py migrate | Applies migrations |
python manage.py createsuperuser | Admin 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 test | Runs tests |
python manage.py shell | Interactive project shell |
How to learn it
- First take the Python course on ShortLesson.
- Create a project and an app with django-admin and manage.py.
- Learn URL routes, views and templates.
- Define models and run migrations.
- Enable the Django admin and configure how models are shown.
- Add forms with validation and authentication.
- Build an API with Django REST Framework.
- Write tests and prepare the project for deployment with PostgreSQL.
Interview questions
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_relatedfor foreign keys andprefetch_relatedfor many relations.2. How do migrations work?
makemigrationscompares models with the current state and writes a change file,migrateapplies it to the database. Migration files are committed to the repository.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
Related technologies
- PythonA readable general-purpose language: web, data, automation and machine learning.
- FastAPIA modern Python API framework built on type hints, with automatic validation and OpenAPI docs.
- FlaskA Python microframework: a minimal core with routing and templates, the rest through extensions.
- PostgreSQLA powerful open source relational database with extensions, JSONB, full-text search and strong SQL support.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).