Django Blog Project
This chapter takes you from scratch to create a blog project with Django and understand the project structure and request lifecycle.
Environment Preparation
Django is a Python Web framework, so you need to confirm the Python environment first.
Open a terminal and check the Python version (3.8 or above required):
$ 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 that do not interfere with each other.
$ python3 -m venv venv # 创建虚拟环境
$ source venv/bin/activate # 激活虚拟环境(Windows:venv\Scripts\activate)
(venv) $ pip install django # 在虚拟环境中安装 Django
Verify the 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/activateto activate the virtual environment. The part before the terminal prompt(venv)indicates that you are currently in the virtual environment.
Create a Django project
Django provides the django-admin startproject command to initialize a project.
(venv) $ django-admin startproject blog_project
(venv) $ cd blog_project
After execution, the following directory structure is 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 Dev 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/
Visit http://127.0.0.1:8000/ in the browser and see Django's welcome page, indicating a successful start.
The development server has built-in hot reload: after modifying and saving code, the server automatically restarts. Press Ctrl+C to stop the server.
Key configuration descriptions in settings.py
settings.py is the central configuration file of a Django project, and 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 (SQLite by default, no extra installation needed) |
| INSTALLED_APPS | List of built-in apps | Apps registered in 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 a Chinese environment:
Example
File path: blog_project/settings.py
LANGUAGE_CODE
=
'zh-hans'
Display Chinese in the admin interface
TIME_ZONE
=
'Asia/Shanghai'
Set the time zone to Beijing time
Create the first app: blog
Django's project organization is that a 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 # 后台管理配置(第三章用到)
├── apps.py # 应用配置
├── models.py # 数据库模型(第二章用到)
├── views.py # 视图函数(本章用到)
├── urls.py # 需要手动创建(应用级路由)
└── migrations/ # 数据库迁移文件(第二章用到)
After creating an 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 the blog app
]
Django request lifecycle
Understanding how requests flow through Django helps quickly locate problems later.
Enter a URL in the browser → urls.py matches the route → views.py processes the logic → templates render HTML → return to the browser.
These three steps (URL → View → Template) are the skeleton pattern of Django development.

Hands-on: The first Django page
Step 1: Write the view function (views.py)
Example
File path: blog/views.py
from
django.
shortcuts
import
render
View function: receives request, returns response
def
index
(
request
)
:
The role of render: combine request + template + data, return an HTML response
return
render
(
request
,
'blog/index.html'
,
{
'welcome'
:
'Welcome to the RUNOOB Blog'
,
'intro'
:
'This is a personal blog showcase built with Django.'
}
)
Step 2: Create the template file (templates)
Create under the blog/ directory templates/blog/ a subdirectory, then create index.html。
Example
RUNOOB Blog
{{ welcome }}
{{ intro }}
Django template variables use
{{ 变量名 }}syntax, the same as Vue3's interpolation expressions. The dictionary data passed to render in views.py can be used directly in the template.
Step 3: Configure the route (urls.py)
First create under blog/ urls.py as the app-level route.
Example
File path: blog/urls.py
from
django.
urls
import
path
from
.
import
views
App route table
urlpatterns
=
[
path
(
''
,
views.
index
,
name
=
'index'
)
,
Empty path matches / homepage
]
Then in the project-level urls.py, use include() to include the app routes:
Example
File path: blog_project/urls.py
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: creating a virtual environment and installing Django, initializing the project with startproject/startapp, and writing the complete chain of View → Template → URL.
Key takeaway: understand Django's project/app hierarchy and master the URL → View → Template three-step development pattern.