Django Structure — MTV Pattern
Django is a full-stack web framework with a "batteries included" philosophy. Admin panel, ORM, authentication, and form processing are all built in.
Django vs FastAPI
| Item | Django | FastAPI |
|---|---|---|
| Philosophy | Full-stack, batteries included | Micro, modular |
| ORM | Built-in Django ORM | Optional (SQLAlchemy, etc.) |
| Admin | Built-in Admin | None |
| Async | Partial (Django 4.1+) | Native async |
| Auto docs | None (drf-spectacular) | Auto OpenAPI |
| Learning curve | Steep | Gentle |
| Best for | CMS, admin panels, large services | API servers, microservices |
Installation and Project Setup
pip install django djangorestframework
# Create project
django-admin startproject myproject .
# Create apps
python manage.py startapp users
python manage.py startapp products
# Dev server
python manage.py runserver
# DB migration
python manage.py makemigrations
python manage.py migrate
# Create superuser
python manage.py createsuperuser
MTV Pattern
Request
│
▼
urls.py ← URL routing
│
▼
View ← Business logic
│
├── Model ← Database access (ORM)
│
▼
Template ← HTML rendering (or JSON serialization)
│
▼
Response
MVC (general) Django MTV
Controller → View
Model → Model
View → Template
Project Structure
myproject/
├── manage.py
├── myproject/
│ ├── settings/
│ │ ├── base.py # common settings
│ │ ├── development.py # dev environment
│ │ └── production.py # production environment
│ ├── urls.py
│ ├── asgi.py
│ └── wsgi.py
├── users/
│ ├── models.py
│ ├── views.py
│ ├── urls.py
│ ├── serializers.py
│ ├── admin.py
│ └── tests.py
└── products/
├── models.py
├── views.py
└── ...
Settings Split Strategy
# myproject/settings/base.py
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"corsheaders",
"users",
"products",
]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
AUTH_USER_MODEL = "users.User"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# myproject/settings/development.py
from .base import *
DEBUG = True
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
CORS_ALLOW_ALL_ORIGINS = True
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
# myproject/settings/production.py
from .base import *
import os
DEBUG = False
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
ALLOWED_HOSTS = os.environ["ALLOWED_HOSTS"].split(",")
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ["DB_NAME"],
"USER": os.environ["DB_USER"],
"PASSWORD": os.environ["DB_PASSWORD"],
"HOST": os.environ["DB_HOST"],
"PORT": "5432",
}
}
SECURE_HSTS_SECONDS = 31536000
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
# Select environment
export DJANGO_SETTINGS_MODULE=myproject.settings.development
python manage.py runserver
URL Configuration
# myproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("api/v1/users/", include("users.urls")),
path("api/v1/products/", include("products.urls")),
]
Summary
| Concept | Description |
|---|---|
| MTV | Model-Template-View pattern |
startproject | Create project skeleton |
startapp | Create feature app |
settings/ split | base / development / production |
INSTALLED_APPS | Register apps |
AUTH_USER_MODEL | Specify custom User model |
Django allows rapid development by following conventions.