Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.
Why Django?
Django follows the "batteries included" philosophy — it provides almost everything developers need out of the box:
- ORM — Database abstraction layer that lets you work with Python objects instead of SQL
- Admin — Auto-generated admin interface for your data models
- Authentication — Built-in user authentication, sessions, permissions
- URL Routing — Clean, expressive URL dispatching
- Template Engine — Powerful template system with inheritance and filters
- Security — Protection against SQL injection, XSS, CSRF, clickjacking
- Forms — Form handling with validation and CSRF protection
Django vs Other Frameworks
| Feature | Django | Flask | FastAPI |
|---|---|---|---|
| Type | Full-stack | Micro | Micro + Async |
| ORM | Built-in | Extensions | Extensions |
| Admin | Built-in | None | None |
| Auth | Built-in | Extension | Extension |
| Async | Since 3.1 | Limited | Native |
| Learning curve | Moderate | Low | Low |
| Best for | Large apps | Small apps | APIs |
The MVT Pattern
Django uses the Model-View-Template pattern:
Code
URL → View → Template (HTML response)
↑
Model (database)- Models define your data structure and handle database operations
- Views contain the business logic — they process requests and return responses
- Templates are HTML files with Django template language for dynamic content
Your First Django Project
Bash
# Install Django
pip install django
# Create a new project
django-admin startproject mysite
# Create an app within the project
cd mysite
python manage.py startapp blog
# Run the development server
python manage.py runserverProject Structure
Code
mysite/
├── manage.py # Command-line utility
├── mysite/
│ ├── __init__.py
│ ├── settings.py # Project settings
│ ├── urls.py # Root URL configuration
│ ├── asgi.py
│ └── wsgi.py
└── blog/
├── __init__.py
├── admin.py # Admin configuration
├── apps.py
├── models.py # Database models
├── tests.py
├── views.py # View functions
└── migrations/Settings Basics
Python
# mysite/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', # Your app
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}The manage.py Command
The manage.py file is your Swiss Army knife:
Bash
python manage.py runserver # Start dev server
python manage.py makemigrations # Create database migrations
python manage.py migrate # Apply migrations
python manage.py createsuperuser # Create admin user
python manage.py shell # Interactive Python shell
python manage.py test # Run tests💡 Tip: Django's development server auto-reloads when you change code. No need to restart it manually.