Django blog project
This chapter takes you from scratch, using Django to create a blog project, and understand the project structure and request lifecycle.
Environment Preparation
Django is a Python web framework; you need to confirm the Python environment first.
Open the terminal and check the Python version (requires 3.8 or higher):
$ python3 --version Python 3.12.0
Create a virtual environment and install Django
A virtual environment is a standard practice for Python projects, giving each project independent dependencies without interfering with each other.
$ python3 -m venv venv # 创建虚拟环境 $ source venv/bin/activate # 激活虚拟环境(Windows:venv\Scripts\activate) (venv) $ pip install django # 在虚拟环境中安装 Django
Verify whether the installation was successful:
(venv) $ python -m django --version 5.0
Every time you open a new terminal, you need to execute it first
source venv/bin/activateActivate the virtual environment. The terminal prompt in front(venv)Indicates that you are currently in the virtual environment.
Create a Django project
Django providesdjango-admin startprojectUse the 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 # 项目管理入口(启动服务器、执行迁移、创建 admin 等) ├── blog_project/ # 项目配置包 │ ├── __init__.py # 标识这是一个 Python 包 │ ├── settings.py # 全局配置(数据库、中间件、模板、静态文件等) │ ├── urls.py # 根 URL 路由表 │ ├── wsgi.py # WSGI 部署入口(生产环境用) │ └── asgi.py # ASGI 部署入口(WebSocket 用)
Start the development server
(venv) $ python manage.py runserver Watching for file changes with StatReloader Performing 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; seeing Django's welcome page means the startup was successful.
The development server comes with hot reload: after saving modified code, the server restarts automatically. Press Ctrl+C to stop the server.
Key configuration notes for settings.py
settings.pyis the central configuration file of a Django project, and each configuration item has a clear meaning.
| Configuration Item | Default value | Function |
|---|---|---|
| DEBUG | True | Development mode (must be set to False after deployment) |
| DATABASES | SQLite | Database configuration (default SQLite, no additional installation required) |
| INSTALLED_APPS | List of built-in applications | Applications 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 the language and time zone to the Chinese environment:
Example
LANGUAGE_CODE = 'zh-hans' # Admin interface displays Chinese
TIME_ZONE = 'Asia/Shanghai' # Set timezone to Beijing Time
Create the first app: blog
Django's project organization isA project contains multiple apps.。
Each App is responsible for an independent functional module (such as blogs, users, comments).
(venv) $ python manage.py startapp blog
After execution, the blog/ directory is generated:
blog/ ├── __init__.py ├── admin.py # 后台管理配置(第三章用到) ├── apps.py # 应用配置 ├── models.py # 数据库模型(第二章用到) ├── views.py # 视图函数(本章用到) ├── urls.py # 需要手动创建(应用级路由) └── migrations/ # 数据库迁移文件(第二章用到)
After creating the App, you need to register it in settings.py:
Example
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', # Added: register the blog app
]
Django request lifecycle
Understanding how requests flow in Django helps you quickly locate issues later.
浏览deviceInput URL → urls.py matchRouting → views.py processing逻辑 → templates 渲染 HTML → return浏览device。
These three steps (URL → View → Template) are the skeleton pattern of Django development.

Hands-on: Your first Django page
Step 1: Write the view function (views.py)
Example
from django.shortcuts import render
# View function: receives request, returns response
def index(request):
# The role of render: combine request + template + data, return HTML response
return render(request, 'blog/index.html', {
'welcome': 'Welcome to EXAMPLE Blog',
'intro': This is a personal blog showcase site built with Django.
})
Step 2: Create the template file (templates)
Create under the blog/ directorytemplates/blog/a subdirectory, then createindex.html。
Example
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>EXAMPLE Blog</title>
<style>
body { font-family: sans-serif; max-width: 800px; margin: 0 auto; padding: 40px 20px; }
h1 { color: #2c3e50; }
</style>
</head>
<body>
<h1>{{ welcome }}</h1>
<p>{{ intro }}</p>
</body>
</html>
Django template variable usage
{{ variable name }}The syntax is the same as Vue3's interpolation expression. The dictionary data passed to render in views.py can be used directly in the template.
Step 3: Configure routing (urls.py)
First create it under blog/urls.pyAs application-level routing.
Example
from django.urls import path
from . import views
# Application Routing Table
urlpatterns = [
path('', views.index, name='index'), # Empty path matches / Home
]
Then in the project-level urls.py useinclude()Import application routes:
Example
from django.contrib import admin
from django.urls import path, include # Import include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')), # Connect the blog app's routes to the root path
]
Chapter summary
In this chapter, you completed three things: created a virtual environment and installed Django, initialized the project with startproject/startapp, and wrote the complete chain of View → Template → URL.
Core takeaway: understand Django's project/app hierarchy, and master the URL → View → Template three-step development pattern.
other extensions