Getting Started

Django Introduction

Learn Django — the Python web framework for perfectionists with deadlines.

What is Django?

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the "batteries included" philosophy — it comes with everything you need: ORM, authentication, admin interface, form handling, and more.

> "The web framework for perfectionists with deadlines"

Django's Philosophy

  • DRY (Don't Repeat Yourself): Reuse code everywhere
  • Convention over configuration: Sensible defaults
  • Loose coupling: Components are independent
  • Explicit is better than implicit: Python's Zen

The MTV Pattern

Django uses Model-Template-View (MTV), similar to MVC:

  • Model: Data layer (ORM)
  • Template: Presentation layer (HTML)
  • View: Business logic (processes requests)

Getting Started

bash
pip install django
django-admin startproject myproject
cd myproject
python manage.py runserver

Example

python
# Create a new project
# django-admin startproject myproject

# Create an app within the project
# python manage.py startapp blog

# myproject/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',  # Our new app
]

# Run development server
# python manage.py runserver

# Create database migrations
# python manage.py makemigrations
# python manage.py migrate

# Create superuser
# python manage.py createsuperuser

# Every line above is either a comment or a setting, so running this printed
# nothing. INSTALLED_APPS is a real Python list — print it to see the wiring
# you just described:
print("INSTALLED_APPS (" + str(len(INSTALLED_APPS)) + " entries):")
for app in INSTALLED_APPS:
    label = "  (yours)" if not app.startswith("django.") else ""
    print(" -", app + label)
Try it yourself — PYTHON