Skip to content

Commit b238cce

Browse files
author
zmrenwu
committed
Step1: 初始化项目
1 parent d10df03 commit b238cce

File tree

6 files changed

+253
-76
lines changed

6 files changed

+253
-76
lines changed

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ faker = "*"
1515
django-pure-pagination = "*"
1616
elasticsearch = ">=2,<3"
1717
django-haystack = "*"
18+
djangorestframework = "*"
19+
django-filter = "*"
1820

1921
[requires]
2022
python_version = "3"

Pipfile.lock

Lines changed: 81 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,169 @@ master 分支为项目的主分支,每一步关键功能的开发都对应一
2727

2828
## 本地运行
2929

30-
Comming soon~
30+
可以使用 Virtualenv、Pipenv、Docker 等在本地运行项目,每种方式都只需运行简单的几条命令就可以了。
3131

32-
### 线上部署
32+
> **注意:**
33+
>
34+
> 因为博客全文搜索功能依赖 Elasticsearch 服务,如果使用 Virtualenv 或者 Pipenv 启动项目而不想搭建 Elasticsearch 服务的话,请先设置环境变量 `ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no` 以关闭实时索引,否则无法创建博客文章。如果关闭实时索引,全文搜索功能将不可用。
35+
>
36+
> Windows 设置环境变量的方式:`set ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no`
37+
>
38+
> Linux 或者 macOS:`export ENABLE_HAYSTACK_REALTIME_SIGNAL_PROCESSOR=no`
39+
>
40+
> 使用 Docker 启动则无需设置,因为会自动启动一个包含 Elasticsearch 服务的 Docker 容器。
3341
34-
Comming soon~
42+
无论采用何种方式,先克隆代码到本地:
43+
44+
```bash
45+
$ git clone https://github.com/HelloGitHub-Team/HelloDjango-REST-framework-tutorial.git
46+
```
47+
48+
### Virtualenv
49+
50+
1. 创建虚拟环境并**激活虚拟环境**,具体方法可参考基础教程中的:[开始进入 django 开发之旅:使用虚拟环境](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/59/#%E4%BD%BF%E7%94%A8%E8%99%9A%E6%8B%9F%E7%8E%AF%E5%A2%83)
51+
52+
2. 安装项目依赖
53+
54+
```bash
55+
$ cd HelloDjango-rest-framework-tutorial
56+
$ pip install -r requirements.txt
57+
```
58+
59+
3. 迁移数据库
60+
61+
```bash
62+
$ python manage.py migrate
63+
```
64+
65+
4. 创建后台管理员账户
66+
67+
```bash
68+
$ python manage.py createsuperuser
69+
```
70+
71+
具体请参阅基础教程中的 [创作后台开启,请开始你的表演](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/65/)
72+
73+
5. 运行开发服务器
74+
75+
```bash
76+
$ python manage.py runserver
77+
```
78+
79+
6. 浏览器访问 http://127.0.0.1:8000/admin,使用第 4 步创建的管理员账户登录后台发布文章,如何发布文章可参考基础教程中的:[创作后台开启,请开始你的表演](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/65/)
80+
81+
或者执行 fake 脚本批量生成测试数据:
82+
83+
```bash
84+
$ python -m scripts.fake
85+
```
86+
87+
> 批量脚本会清除全部已有数据,包括第 4 步创建的后台管理员账户。脚本会再默认生成一个管理员账户,用户名和密码都是 admin。
88+
89+
9. 浏览器访问:http://127.0.0.1:8000,可进入到博客首页
90+
91+
### Pipenv
92+
93+
1. 安装 Pipenv(已安装可跳过)
94+
95+
```bash
96+
$ pip install pipenv
97+
```
98+
99+
2. 安装项目依赖
100+
101+
```bash
102+
$ cd HelloDjango-rest-framework-tutorial
103+
$ pipenv install --dev
104+
```
105+
106+
关于如何使用 Pipenv,参阅基础教程中:[开始进入 django 开发之旅](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/59/) 的 Pipenv 创建和管理虚拟环境部分。
107+
108+
3. 迁移数据库
109+
110+
在项目根目录运行如下命令迁移数据库:
111+
```bash
112+
$ pipenv run python manage.py migrate
113+
```
114+
115+
4. 创建后台管理员账户
116+
117+
在项目根目录运行如下命令创建后台管理员账户
118+
119+
```bash
120+
$ pipenv run python manage.py createsuperuser
121+
```
122+
123+
具体请参阅基础教程中的 [创作后台开启,请开始你的表演](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/65/)。
124+
125+
5. 运行开发服务器
126+
127+
在项目根目录运行如下命令开启开发服务器:
128+
129+
```bash
130+
$ pipenv run python manage.py runserver
131+
```
132+
133+
6. 浏览器访问 http://127.0.0.1:8000/admin,使用第 4 步创建的管理员账户登录后台发布文章,如何发布文章可参考基础教程中的:[创作后台开启,请开始你的表演](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/65/)。
134+
135+
或者执行 fake 脚本批量生成测试数据:
136+
137+
```bash
138+
$ pipenv run python -m scripts.fake
139+
```
140+
141+
> 批量脚本会清除全部已有数据,包括第 4 步创建的后台管理员账户。脚本会再默认生成一个管理员账户,用户名和密码都是 admin。
142+
143+
7. 在浏览器访问:http://127.0.0.1:8000/,可进入到博客首页。
144+
145+
### Docker
146+
147+
1. 安装 Docker 和 Docker Compose
148+
149+
3. 构建和启动容器
150+
151+
```bash
152+
$ docker-compose -f local.yml build
153+
$ docker-compose -f local.yml up
154+
```
155+
156+
4. 创建后台管理员账户
157+
158+
```bash
159+
$ docker exec -it hellodjango_rest_framework_tutorial_local python manage.py createsuperuser
160+
```
161+
162+
其中 hellodjango_rest_framework_tutorial_local 为项目预定义容器名。
163+
164+
4. 浏览器访问 http://127.0.0.1:8000/admin,使用第 3 步创建的管理员账户登录后台发布文章,如何发布文章可参考基础教程中的:[创作后台开启,请开始你的表演](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/65/)。
165+
166+
或者执行 fake 脚本批量生成测试数据:
167+
168+
```bash
169+
$ docker exec -it hellodjango_rest_framework_tutorial_local python -m scripts.fake
170+
```
171+
172+
> 批量脚本会清除全部已有数据,包括第 3 步创建的后台管理员账户。脚本会再默认生成一个管理员账户,用户名和密码都是 admin。
173+
174+
5. 为 fake 脚本生成的博客文章创建索引,这样就可以使用 Elasticsearch 服务搜索文章
175+
176+
```bash
177+
$ docker exec -it hellodjango_rest_framework_tutorial_local python manage.py rebuild_index
178+
```
179+
180+
> 通过 admin 后台添加的文章会自动创建索引。
181+
182+
6. 在浏览器访问:http://127.0.0.1:8000/,可进入到博客首页。
183+
184+
## 线上部署
185+
186+
拼命撰写中...
35187

36188
## 教程目录索引
37189

38190
1. [开篇](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/)
191+
2. [django-rest-framework 是什么鬼?](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/91/)
192+
3. [初始化 RESTful API 风格的博客系统](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/92/)
39193

40194
## 公众号
41195
<p align="center">

blogproject/settings/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"django.contrib.staticfiles",
3232
"pure_pagination", # 分页
3333
"haystack", # 搜索
34+
"rest_framework",
3435
"blog.apps.BlogConfig", # 注册 blog 应用
3536
"comments.apps.CommentsConfig", # 注册 comments 应用
3637
]

0 commit comments

Comments
 (0)