Skip to content

Django Introduction

This chapter introduces the basic concepts, features, and advantages of the Django framework, helping you understand why Django is one of the preferred Python web development frameworks.

What is Django

Django is a high-level Python web framework, started in 2003 by Adrian Holovaty and Simon Willison, and first publicly released in 2005. Django is named after the famous guitarist Django Reinhardt.

Django's Core Philosophy

Django follows these core principles:

  1. DRY Principle (Don't Repeat Yourself)
  2. Rapid Development - Fast development from concept to completion
  3. Loose Coupling - Django's components are relatively independent
  4. Explicit over Implicit - Code should be clear and readable
python
# Django's design philosophy is reflected in the code
# Example: Explicit URL configuration
from django.urls import path
from . import views

urlpatterns = [
    path('articles/', views.article_list, name='article_list'),
    path('articles/<int:id>/', views.article_detail, name='article_detail'),
]

Django Features and Advantages

1. Complete Web Framework

Django is a "full-stack" framework, providing most functionalities needed for web development:

  • ORM (Object-Relational Mapping) - Database abstraction layer
  • Template Engine - Dynamic HTML generation
  • Form Handling - Data validation and rendering
  • User Authentication - Login, permission management
  • Admin Interface - Automatically generated administration interface
  • Internationalization - Multi-language support
  • Cache Framework - Performance optimization
  • Security Features - Built-in security protection

2. MVT Architecture Pattern

Django uses the MVT (Model-View-Template) architecture pattern:

┌─────────────────────────────────────────────────────────┐
│                    Django MVT Architecture               │
├─────────────────────────────────────────────────────────┤
│  User Request → URL Config → View → Model               │
│                        ↓                                │
│                    Template ← Data                      │
│                        ↓                                │
│                   HTML Response → User                  │
└─────────────────────────────────────────────────────────┘

Model (模型):

  • Defines data structure
  • Handles database operations
  • Business logic

View (视图):

  • Handles user requests
  • Calls models to get data
  • Selects templates to render response

Template (模板):

  • Defines page structure
  • Displays data
  • User interface

3. Powerful ORM System

Django's ORM lets you operate databases using Python code:

python
# Define models
class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.title

# Database operations
# Create
article = Article.objects.create(
    title="Getting Started with Django",
    content="Django is an excellent web framework..."
)

# Query
articles = Article.objects.filter(title__contains="Django")
latest_articles = Article.objects.order_by('-created_at')[:5]

# Update
Article.objects.filter(id=1).update(title="Advanced Django Tutorial")

# Delete
Article.objects.filter(id=1).delete()

4. Automatic Admin Interface

Django provides an automatically generated admin interface:

python
# admin.py
from django.contrib import admin
from .models import Article

@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ['title', 'created_at']
    list_filter = ['created_at']
    search_fields = ['title', 'content']
    date_hierarchy = 'created_at'

5. Built-in Security Features

Django includes multiple built-in security protection mechanisms:

  • CSRF Protection - Cross-Site Request Forgery protection
  • XSS Protection - Cross-Site Scripting protection
  • SQL Injection Protection - ORM automatic escaping
  • Clickjacking Protection - X-Frame-Options header
  • HTTPS Support - SSL/TLS configuration
  • Password Hashing - Secure password storage
python
# Security configuration in settings.py
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

Django vs Other Frameworks

Django vs Flask

FeatureDjangoFlask
TypeFull-stack frameworkMicroframework
Learning CurveSteeperGentle
Built-in FeaturesRichMinimalist
FlexibilityConvention over configurationHighly flexible
Suitable forMedium to large projectsSmall projects, APIs
python
# Django view example
from django.shortcuts import render
from .models import Article

def article_list(request):
    articles = Article.objects.all()
    return render(request, 'articles/list.html', {'articles': articles})

# Flask view example
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/articles')
def article_list():
    # Need to handle database connection and queries manually
    articles = get_articles_from_db()
    return render_template('articles/list.html', articles=articles)

Django vs FastAPI

FeatureDjangoFastAPI
Main UseFull-featured web applicationsAPI development
PerformanceMediumHigh performance
Type HintsPartial supportNative support
Documentation GenerationRequires additional toolsAuto-generated
EcosystemMature and richEmerging but active

Django Use Cases

1. Content Management System (CMS)

Django is excellent for building CMS:

python
# Blog post model
class Post(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    content = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    published = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        ordering = ['-created_at']
    
    def get_absolute_url(self):
        return reverse('post_detail', kwargs={'slug': self.slug})

2. E-commerce Websites

Django's flexibility makes it suitable for e-commerce development:

python
# Product model
class Product(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    price = models.DecimalField(max_digits=10, decimal_places=2)
    stock = models.PositiveIntegerField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='products/')
    
    def is_in_stock(self):
        return self.stock > 0

# Order model
class Order(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    products = models.ManyToManyField(Product, through='OrderItem')
    total_amount = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=20, choices=ORDER_STATUS_CHOICES)

3. Social Network Applications

Django's user system and permission management are suitable for social applications:

python
# User profile model
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True)
    avatar = models.ImageField(upload_to='avatars/', blank=True)
    followers = models.ManyToManyField('self', symmetrical=False, blank=True)
    
    def get_follower_count(self):
        return self.followers.count()

# Post model
class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField(max_length=280)
    likes = models.ManyToManyField(User, related_name='liked_posts', blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

4. API Services

Developing APIs with Django REST Framework:

python
# serializers.py
from rest_framework import serializers
from .models import Article

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id', 'title', 'content', 'created_at']

# views.py
from rest_framework import viewsets
from .models import Article
from .serializers import ArticleSerializer

class ArticleViewSet(viewsets.ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

Django Ecosystem

Core Packages

  • Django - Core framework
  • Django REST Framework - API development
  • Django Channels - WebSocket support
  • Celery - Asynchronous task queue

Common Third-party Packages

python
# requirements.txt example
Django==4.2.7
djangorestframework==3.14.0
django-cors-headers==4.3.1
django-filter==23.3
Pillow==10.0.1
psycopg2-binary==2.9.7
redis==5.0.1
celery==5.3.4

Development Tools

  • Django Debug Toolbar - Debugging tool
  • Django Extensions - Development extensions
  • Factory Boy - Test data generation
  • Coverage.py - Code coverage

Success Stories

Well-known Websites Using Django

  1. Instagram - Social media platform
  2. Pinterest - Image sharing website
  3. Spotify - Music streaming service
  4. YouTube - Video sharing platform (partial functionality)
  5. Dropbox - Cloud storage service
  6. Mozilla - Browser and web services
  7. NASA - NASA website

Enterprise-level Applications

python
# Enterprise configuration example
# settings/production.py
import os
from .base import *

DEBUG = False
ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']

# Database configuration
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'HOST': os.environ.get('DB_HOST'),
        'PORT': os.environ.get('DB_PORT'),
    }
}

# Cache configuration
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379/1',
    }
}

# Static files configuration
STATIC_ROOT = '/var/www/static/'
MEDIA_ROOT = '/var/www/media/'

# Security configuration
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True

Django Version History

Major Version Milestones

  • Django 1.0 (2008) - First stable version
  • Django 1.4 (2012) - Long-term support version
  • Django 1.8 (2015) - LTS, significant improvements
  • Django 2.0 (2017) - Simplified URL configuration
  • Django 3.0 (2019) - Async support
  • Django 4.0 (2021) - Modernization improvements
  • Django 4.2 (2023) - Current LTS version

Version Selection Recommendations

python
# Recommended version selection
# Production environment: Use LTS versions
Django==4.2.7  # Current LTS, supported until April 2026

# New projects: Can use latest stable version
Django==4.2.7  # Latest stable version

# Learning: Use LTS versions
Django==4.2.7  # Well-documented, good community support

Learning Path for Django

1. Foundation Stage

  • Master Python basic syntax
  • Understand basic web development concepts
  • Learn HTML, CSS basics
  • Understand HTTP protocol

2. Beginner Stage

  • Django basic concepts
  • Create first project
  • Models, views, templates
  • URL configuration

3. Intermediate Stage

  • Form handling
  • User authentication
  • Admin interface
  • Static file management

4. Advanced Stage

  • Class-based views and generic views
  • REST API development
  • Cache and performance optimization
  • Deployment and operations

5. Expert Stage

  • Custom middleware
  • Signal system
  • Testing strategies
  • Architecture design

Chapter Summary

Django is a powerful, elegantly designed Python web framework with the following characteristics:

Core Advantages:

  • Complete full-stack solution
  • Powerful ORM system
  • Automatic admin interface
  • Built-in security features
  • Rich ecosystem

Suitable Use Cases:

  • Content management systems
  • E-commerce websites
  • Social network applications
  • Enterprise web applications
  • RESTful API services

Learning Value:

  • Increases development efficiency
  • Ensures code quality
  • Good career prospects
  • Active community support

In the next chapter, we will learn how to set up a Django development environment, including the installation and configuration of Python, Django, and related tools.

Further Reading

[file content begin]

First Django Project

This chapter will guide you through creating your first Django project, understanding the basic structure of a Django project, and running your first web application.

Creating a Django Project

Using django-admin to Create a Project

bash
# Ensure virtual environment is activated
(venv) $ django-admin startproject mysite

# View created project structure
cd mysite
tree .  # Linux/macOS
# or
dir /s  # Windows

Project Structure Analysis

mysite/
├── manage.py           # Django project management script
└── mysite/            # Project configuration package
    ├── __init__.py    # Python package identifier file
    ├── settings.py    # Project settings file
    ├── urls.py        # URL configuration file
    ├── asgi.py        # ASGI configuration file
    └── wsgi.py        # WSGI configuration file

manage.py Explained

python
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

def main():
    """Run administrative tasks."""
    # Set Django settings module
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

if __name__ == '__main__':
    main()

Main uses of manage.py:

  • Run development server
  • Create applications
  • Execute database migrations
  • Create superuser
  • Collect static files

settings.py Explained

python
# mysite/settings.py
"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 4.2.7.
"""

from pathlib import Path

# Project root directory
BASE_DIR = Path(__file__).resolve().parent.parent

# Security key - should be kept secret in production
SECRET_KEY = 'django-insecure-your-secret-key-here'

# Debug mode - should be set to False in production
DEBUG = True

# Allowed host list
ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',          # Admin interface
    'django.contrib.auth',           # Authentication system
    'django.contrib.contenttypes',   # Content type framework
    'django.contrib.sessions',       # Session framework
    'django.contrib.messages',       # Message framework
    'django.contrib.staticfiles',    # Static file management
]

# Middleware configuration
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# URL configuration
ROOT_URLCONF = 'mysite.urls'

# Template configuration
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# WSGI application
WSGI_APPLICATION = 'mysite.wsgi.application'

# Database configuration
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Password validation
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
LANGUAGE_CODE = 'zh-hans'  # Chinese
TIME_ZONE = 'Asia/Shanghai'  # China timezone
USE_I18N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = 'static/'

# Default primary key field type
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

urls.py Explained

python
# mysite/urls.py
"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

Running the Development Server

Start the Server

bash
# In the project root directory (contains manage.py)
python manage.py runserver

# Output information
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

December 15, 2023 - 10:30:00
Django version 4.2.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Access the Website

Open your browser and visit http://127.0.0.1:8000/, you should see the Django welcome page:

html
<!-- Django welcome page content -->
The install worked successfully! Congratulations!

You are seeing this message because this Django installation is working correctly.

Next, start your first app by running python manage.py startapp [app_label].

Custom Server Configuration

bash
# Specify port
python manage.py runserver 8080

# Specify IP and port
python manage.py runserver 0.0.0.0:8000

# Use different settings file
python manage.py runserver --settings=mysite.settings_dev

Database Initialization

Apply Initial Migrations

bash
# Apply database migrations
python manage.py migrate

# Output information
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

Create Superuser

bash
# Create administrator account
python manage.py createsuperuser

# Enter information as prompted
Username (leave blank to use 'yourname'): admin
Email address: admin@example.com
Password:
Password (again):
Superuser created successfully.

Access Admin Interface

Visit http://127.0.0.1:8000/admin/ and log in using the superuser account you just created.

Creating Your First App

Django Project vs Application

Project
├── App1 - Blog functionality
├── App2 - User management
├── App3 - Comment system
└── Project configuration

Project: Container for the entire website Application: Python package that implements specific functionality

Create Application

bash
# Create an app named polls
python manage.py startapp polls

# View app structure
tree polls/  # Linux/macOS

Application Structure Analysis

polls/
├── __init__.py        # Python package identifier
├── admin.py          # Admin interface configuration
├── apps.py           # Application configuration
├── migrations/       # Database migration files
│   └── __init__.py
├── models.py         # Data models
├── tests.py          # Test files
└── views.py          # View functions

Application Files Explained

apps.py - Application Configuration

python
# polls/apps.py
from django.apps import AppConfig

class PollsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'polls'
    verbose_name = 'Polling App'  # Name displayed in admin interface

models.py - Data Models

python
# polls/models.py
from django.db import models

# Create your models here.

views.py - View Functions

python
# polls/views.py
from django.shortcuts import render

# Create your views here.

admin.py - Admin Interface

python
# polls/admin.py
from django.contrib import admin

# Register your models here.

Writing Your First View

Create View Functions

python
# polls/views.py
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

def detail(request, question_id):
    return HttpResponse(f"You're looking at question {question_id}.")

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse(f"You're voting on question {question_id}.")

Configure URL Routing

1. Create Application URL Configuration

python
# polls/urls.py (new file)
from django.urls import path
from . import views

urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

2. Include Application URLs in Project URL Configuration

python
# mysite/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

Register Application

python
# mysite/settings.py
INSTALLED_APPS = [
    'polls.apps.PollsConfig',  # Add polls app
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Test Views

bash
# Start development server
python manage.py runserver

# Visit the following URLs to test
http://127.0.0.1:8000/polls/
http://127.0.0.1:8000/polls/34/
http://127.0.0.1:8000/polls/34/results/
http://127.0.0.1:8000/polls/34/vote/

URL Configuration Details

URL Pattern Syntax

python
from django.urls import path, re_path
from . import views

urlpatterns = [
    # Basic path
    path('', views.index),
    
    # Path with parameters
    path('articles/<int:year>/', views.year_archive),
    path('articles/<int:year>/<int:month>/', views.month_archive),
    path('articles/<slug:slug>/', views.article_detail),
    
    # Regular expression path
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    
    # Optional parameters
    path('blog/', views.blog_index),
    path('blog/page/<int:num>/', views.blog_index),
]

Path Converters

python
# Django built-in path converters
str     # Matches any non-empty string except '/'
int     # Matches positive integers, including 0
slug    # Matches letters, numbers, hyphens, underscores
uuid    # Matches formatted UUID
path    # Matches any non-empty string, including path separator '/'

# Usage examples
urlpatterns = [
    path('articles/<int:year>/', views.year_archive),
    path('articles/<str:title>/', views.article_detail),
    path('users/<uuid:user_id>/', views.user_profile),
    path('files/<path:file_path>/', views.file_download),
]

Custom Path Converters

python
# converters.py
class FourDigitYearConverter:
    regex = '[0-9]{4}'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return '%04d' % value

# urls.py
from django.urls import path, register_converter
from . import views, converters

register_converter(converters.FourDigitYearConverter, 'yyyy')

urlpatterns = [
    path('articles/<yyyy:year>/', views.year_archive),
]

URL Naming and Reverse Resolution

python
# urls.py
from django.urls import path
from . import views

app_name = 'polls'  # Application namespace
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]
python
# Using in views
from django.urls import reverse
from django.http import HttpResponseRedirect

def vote(request, question_id):
    # ... voting logic ...
    return HttpResponseRedirect(reverse('polls:results', args=(question_id,)))
html
<!-- Using in templates -->
<a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a>

Project Configuration Optimization

Environment Variable Configuration

python
# settings.py
import os
from pathlib import Path

# Read configuration from environment variables
DEBUG = os.environ.get('DEBUG', 'False').lower() == 'true'
SECRET_KEY = os.environ.get('SECRET_KEY', 'your-default-secret-key')
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',')

# Database configuration
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# If DATABASE_URL environment variable is set, use it
DATABASE_URL = os.environ.get('DATABASE_URL')
if DATABASE_URL:
    import dj_database_url
    DATABASES['default'] = dj_database_url.parse(DATABASE_URL)

Separate Configuration Files

mysite/
├── settings/
│   ├── __init__.py
│   ├── base.py        # Base configuration
│   ├── development.py # Development environment configuration
│   ├── production.py  # Production environment configuration
│   └── testing.py     # Testing environment configuration
python
# 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',
    'polls.apps.PollsConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'

# ... other common configuration
python
# settings/development.py
from .base import *

DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Development tools
INSTALLED_APPS += [
    'debug_toolbar',
    'django_extensions',
]

MIDDLEWARE += [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

INTERNAL_IPS = ['127.0.0.1']
python
# settings/production.py
from .base import *
import os

DEBUG = False
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'HOST': os.environ.get('DB_HOST', 'localhost'),
        'PORT': os.environ.get('DB_PORT', '5432'),
    }
}

# Security configuration
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'

Using Different Configurations

bash
# Development environment
python manage.py runserver --settings=mysite.settings.development

# Production environment
python manage.py migrate --settings=mysite.settings.production

# Or set environment variable
export DJANGO_SETTINGS_MODULE=mysite.settings.development
python manage.py runserver

Common Management Commands

Project Management Commands

bash
# Create project
django-admin startproject mysite

# Create application
python manage.py startapp myapp

# Run development server
python manage.py runserver
python manage.py runserver 8080
python manage.py runserver 0.0.0.0:8000

# Check project
python manage.py check
python manage.py check --deploy  # Deployment check

Database Management Commands

bash
# Create migration files
python manage.py makemigrations
python manage.py makemigrations myapp

# Apply migrations
python manage.py migrate
python manage.py migrate myapp

# View migration status
python manage.py showmigrations

# View SQL statements
python manage.py sqlmigrate myapp 0001

User Management Commands

bash
# Create superuser
python manage.py createsuperuser

# Change user password
python manage.py changepassword username

Other Useful Commands

bash
# Collect static files
python manage.py collectstatic

# Clean sessions
python manage.py clearsessions

# Enter Django shell
python manage.py shell

# Run tests
python manage.py test
python manage.py test myapp

# Generate secret key
python manage.py shell -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"

Project Deployment Preparation

Production Environment Checklist

python
# Security check
python manage.py check --deploy

# Common check items
SECURITY_WARNING = [
    'DEBUG = False',
    'SECRET_KEY must not be exposed',
    'ALLOWED_HOSTS must be set',
    'HTTPS configuration',
    'CSRF protection',
    'XSS protection',
]

requirements.txt

bash
# Generate dependency file
pip freeze > requirements.txt

# requirements.txt content example
Django==4.2.7
psycopg2-binary==2.9.7
gunicorn==21.2.0
whitenoise==6.6.0
python-decouple==3.8

Static File Configuration

python
# settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

STATICFILES_DIRS = [
    BASE_DIR / 'static',
]

# Use WhiteNoise to handle static files
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',  # Add this line
    # ... other middleware
]

# Static file compression
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Chapter Summary

This chapter detailed how to create your first Django project:

Key Points:

  • Project Creation: Use django-admin startproject to create a project
  • Project Structure: Understand Django project file organization
  • Application Creation: Use manage.py startapp to create an app
  • URL Configuration: Set up URL routing and view mapping
  • Development Server: Run and test Django application

Important Concepts:

  • Project vs Application: Project is a container, applications are functional modules
  • MVT Architecture: Model-View-Template design pattern
  • URL Routing: Map URLs to view functions
  • Configuration Management: Manage project configuration using settings.py

Best Practices:

  • Create separate applications for each functionality
  • Use meaningful URL patterns and names
  • Separate configuration files for different environments
  • Use environment variables to manage sensitive information
  • Follow Django's project structure conventions

Development Workflow:

  1. Create project and applications
  2. Configure URL routing
  3. Write view functions
  4. Test functionality
  5. Optimize configuration

In the next chapter, we will dive deeper into Django's fundamental concepts, including settings, middleware, application configuration, and other core knowledge.

Further Reading

Content is for learning and research only.