This chapter takes you from scratch to create a blog project with Django, understanding the project structure and request lifecycle.
* * *
## Environment Setup
Django is a Python Web framework, you need to confirm the Python environment first.
Open the terminal, check Python version (requires 3.8 or higher):
$ python3 --version Python 3.12.0
### Create Virtual Environment and Install Django
Virtual environment is the standard practice for Python projects, allowing each project to have independent dependencies without interference.
$ python3 -m venv venv # Create virtual environment $ source venv/bin/activate # Activate virtual environment (Windows: venv\Scripts\activate) (venv) $ pip install django # Install Django in virtual environment
Verify installation was successful:
(venv) $ python -m django --version 5.0
> Every time you open a new terminal, you need to first execute `source venv/bin/activate` to activate the virtual environment. The `(venv)` in front of the terminal prompt indicates that you are currently in a virtual environment.
* * *
## Create Django Project
Django provides the django-admin startproject command to initialize the project.
(venv) $ django-admin startproject blog_project (venv) $ cd blog_project
After execution, the following directory structure will be generated:
blog_project/βββ manage.py # Project management entry (start server, execute migrations, create admin, etc.)βββ blog_project/ # Project configuration packageβ βββ __init__.py # Marks this as a Python packageβ βββ settings.py # Global configuration (database, middleware, templates, static files, etc.)β βββ urls.py # Root URL routing tableβ βββ wsgi.py # WSGI deployment entry (for production)β βββ asgi.py # ASGI deployment entry (for WebSocket)
### Start Development Server
(venv) $ python manage.py runserver Watching for file changes with StatReloaderPerforming system checks...System check identified no issues (0 silenced).Starting development server at http://127.0.0.1:8000/
Access http://127.0.0.1:8000/ in the browser, and you will see Django's welcome page, indicating successful startup.
> The development server has hot reload: after modifying code and saving, the server will automatically restart. Press Ctrl+C to stop the server.
* * *
## settings.py Key Configuration Explanation
settings.py is the central configuration file for Django projects, each configuration item has a clear meaning.
| Configuration Item | Default Value | Purpose |
| --- | --- | --- |
| DEBUG | True | Development mode (must be set to False after going live) |
| DATABASES | SQLite | Database configuration (default SQLite, no additional installation needed) |
| INSTALLED_APPS | Built-in app list | Apps registered to the project |
| TEMPLATES | DjangoTemplates | Template engine configuration |
| LANGUAGE_CODE | 'en-us' | Language (can be changed to 'zh-hans') |
| TIME_ZONE | 'UTC' | Time zone (can be changed to 'Asia/Shanghai') |
Change language and time zone to Chinese environment:
## Example
# File path: blog_project/settings.py
LANGUAGE_CODE ='zh-hans'# Chinese display for admin interface
TIME_ZONE ='Asia/Shanghai'# Time zone set to Beijing time
* * *
## Create First App: blog
Django's project organization method is: Project contains multiple Apps.
Each App is responsible for an independent functional module (such as blog, user, comments).
(venv) $ python manage.py startapp blog
After execution, the blog/ directory is generated:
blog/βββ __init__.py βββ admin.py # Admin configuration (used in Chapter 3)βββ apps.py # App configurationβββ models.py # Database models (used in Chapter 2)βββ views.py # View functions (used in this chapter)βββ urls.py # Need to create manually (app-level routing)βββ migrations/ # Database migration files (used in Chapter 2)
After creating the App, you need to register it in settings.py:
## Example
# File path: blog_project/settings.py
INSTALLED_APPS =[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',# New: register blog app
]
* * *
## Django Request Lifecycle
Understanding the flow of requests in Django helps quickly locate issues in the future.
Browser enters URL β urls.py matches route β views.py processes logic β templates render HTML β returns to browser.
These three steps (URL β View β Template) are the skeleton pattern of Django development.
!(https://example.com/wp-content/uploads/2026/05/5c50af42-6be1-44e5-9baf-3eb955bcdb8f.webp)
* * *
## Hands-on: First Django Page
### Step 1: Write View Function (views.py)
## Example
# File path: blog/views.py
from django.shortcuts import render
# View function: receives request, returns response
def index(request):
# render's role: combines request + template + data, returns HTML response
return render(request,'blog/index.html',{
'welcome': 'Welcome to TUTORIAL Blog',
'intro': 'This is a personal blog showcase built with Django.'
})
### Step 2: Create Template File (templates)
Create `templates/blog/` subdirectory under blog/ directory, then create `index.html`.
## Example
TUTORIAL Blog
{{ welcome }}
{{ intro }}
> Django template variables use `{{ variable name }}` syntax, same as Vue3's interpolation expression. The dictionary data passed to