Chuyển đến nội dung chính

Lesson 1: Introducing Django - The Web Framework for Perfectionists

What is Django, the "batteries included" philosophy? Compare Django vs FastAPI vs Flask vs Laravel. MVT architecture, Django ecosystem. Install and create your first project and app.

💻 Programming — Lesson 1 Lesson 1: Introducing Django - The Web Framework for Perfectionists

Django: From Basics to Advanced

Part 1: Django Fundamentals

xdev.asia

1. What is Django?

Django is a Python web framework "batteries included" — providing an ORM, admin panel, authentication, form processing, template engine and many other modules right from the start.

FeaturesDjangoFastAPIFlaskLaravel
LanguagePythonPythonPythonPHP
TypeFull-stackAPI-onlyMicrophoneFull-stack
ORMBuilt-inSQLAlchemySQLAlchemyEloquent
AdminBuilt-inNoNoThird party
AsyncDjango 5.xNativeLimitedNo
Use casesFull-stack, CMSAPIs, microservicesSimple appsFull-stack

2. MVT Architecture

Request → URL Router → View → Template → Response
                         ↕
                       Model ↔ Database
ComponentRole
ModelData layer — ORM, database schema
ViewBusiness logic — process requests, return responses
TemplatePresentation — HTML rendering

3. Install & Create Project

# Python virtual environment
python -m venv .venv
source .venv/bin/activate

# Cài Django
pip install django djangorestframework

# Tạo project
django-admin startproject config .

# Tạo app
python manage.py startapp users
python manage.py startapp products

4. Project structure

myproject/
├── config/
│   ├── __init__.py
│   ├── settings.py      # Configuration
│   ├── urls.py           # Root URL routing
│   ├── wsgi.py           # WSGI entry
│   └── asgi.py           # ASGI entry (async)
├── users/
│   ├── models.py         # Data models
│   ├── views.py          # View logic
│   ├── urls.py           # App URL routing
│   ├── serializers.py    # DRF serializers
│   ├── admin.py          # Admin config
│   └── tests.py          # Tests
├── manage.py
└── requirements.txt

5. Basic settings

# config/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'rest_framework',
    # Custom apps
    'users',
    'products',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'postgres',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
# Chạy migrations & server
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

Next article: Models & Django ORM — fields, relationships, managers.