从零开始写博客系统——获取博客列表
开始
从现在开始,我们就要开始进行后台的开发了,首先我们要用Flask
开启我们的第一个接口。
参考大部分博客的情况,打开博客首页的时候都会需要展示博客的列表。因此我们的第一个后台接口,就是这个博客列表。
第一个接口
我们在环境的根目录下新建一个文件:app.py
在这个文件中写入下面的信息。
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
2
3
4
5
6
7
然后到终端执行命令
source bin/activate
flask run
$ flask run
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
2
3
4
5
6
7
8
9
这个时候,博客的后台服务就抛起来了,就可以打开浏览器输入地址:http://127.0.0.1:5000
就可以看到一个HelloWorld
的界面。
在终端按ctrl+c
就可以关闭这个服务
确定文章的构成
我们按照简单来设定,一篇文章包含的要素有以下几点:标题、作者、文章内容、创建时间、修改时间、分类、标签。除了这些文章的元素之外,还会有一个文章的ID。
编写数据文件
在一开始上手的阶段,就去接触数据库并不是一个好的决定,这会分散大家的注意力。我希望大家能够把主要的精力先放在我们的博客系统上,因此我们会在初期使用数据文件来模拟这部分数据。
按照上面文章的构成,我们编写数据文件的时候就可以按照这个要求来模拟博客文章。
新建一个data.py文件,然后写入以下内容:
article_list = [
{
"id": 1
"title": "开篇",
"content": "我从2015年底开始慢慢的学习编码相关的知识。在这个博客我把自己学习的记录全部都记了下来,这么多年过去了,版本不停的在更替,当年的那个系列确实已经没啥参考意义了,并且受限于当时的水平,写出来的东西也没啥价值,甚至在某些时候会误导初学者,因此决定重新写一下这个系列的内容,如果可以的话,也配套录制一些视频,方便后来人学习。",
"author": "点点寒彬",
"created_time": "2022-04-01 11:12:21",
"modified_time": "2022-04-23 11:12:21",
"category": "从零开始学博客",
"tag": [
"Python",
"Flask",
"博客"
]
},{
"id": 2
"title": "环境搭建",
"content": "关于Python的安装,环境变量等配置这里就不单独出文章了,网上有大量的文章可以参考。我使用的Mac电脑,使用Windows也是同样可以的。只不过某些配置可能略有差异。",
"author": "点点寒彬",
"created_time": "2022-04-05 11:12:21",
"modified_time": "2022-04-23 11:12:21",
"category": "从零开始学博客",
"tag": [
"Python",
"Flask",
"博客"
]
}
]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
这里我们按照之前设计的文章结构来写了一些模拟的数据。
编写文章类
新建article.py
,输入下面的信息:
# coding=utf-8
class Article(object):
def __init__(
self,
id: int,
title: str,
content: str,
author: str,
created_time: str,
modified_time: str,
category: str,
tag: list
):
self.id = id
self.title = title
self.content = content
self.author = author
self.created_time = created_time
self.modified_time = modified_time
self.category = category
self.tag = tag
def to_dict(self):
return {
"id": self.id,
"title": self.title,
"content": self.content,
"author": self.author,
"created_time": self.created_time,
"modified_time": self.modified_time,
"category": self.category,
"tag": self.tag
}
class Articles(object):
def __init__(self):
self.articles = []
self.total = 0
def append(self, article: Article):
self.articles.append(article.to_dict())
self._calc_total()
def append_list(self, articles: list):
self.articles = self.articles + articles
self._calc_total()
def _calc_total(self):
self.total = len(self.articles)
def to_json(self):
return {
"total": self.total,
"articles": self.articles
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
这里我们定义了两个类,Article
用来装载文章相关的数据,Articles
用来装文章列表以及计算文章总数。
编写查询列表接口
到这里终于到本文的正题了,获取文章列表。
from flask import Flask, jsonify
import data
import article
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
@app.route("/article_list")
def article_list():
articles = article.Articles()
for x in data.article_list:
at = article.Article(
x.get("id"),
x.get("title"),
x.get("content"),
x.get("author"),
x.get("created_time"),
x.get("modified_time"),
x.get("category"),
x.get("tag")
)
articles.append(at)
return jsonify(articles.to_json())
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
我们把模拟的数据放到一个一个放到里面后,在接口处返回。
在终端执行命令 flask run
后,打开浏览器输入 http://127.0.0.1:5000/article_list 就可以看到我们返回的json
数据了。
代码仓库从管理代码
上一章提到了我们的代码会在代码仓库中管理。我们是在本篇新写的代码,因此需要重新见一个代码仓库。
在项目跟路径下执行命令:
git init
>>
Initialized empty Git repository in xxxx
2
3
4
然后添加一个.gitignore文件在根目录,输入如下信息,这个表示这些文件或者文件夹不要放到代码仓库。
__pycache__
bin
include
lib
*.cfg
2
3
4
5
这些依赖的库不在代码仓库管理,但是为了能让Python的依赖在其他地方使用的时候,依然是一样的版本,因此我们需要锁定我们现在依赖的信息。
pip freeze > requirements.txt
执行后会发现,我们的跟目录多了一个requirements.txt
文件,这里会记录我们的依赖包的版本信息。(如果读者用我的requirements.txt安装失败了,那么应该是Python的版本跟我的有差异,直接执行 pip install flask
即可)
现在我们把这些内容添加到代码仓库中。
git add app.py
git add data.py
git add article.py
git add requirements.txt
git add .gitignore
2
3
4
5
再执行命令 git status -s
可以看到我们的文件前面多了一个A,表示添加到暂存区了。
blog git:(master) ✗ git status -s
A .gitignore
A app.py
A article.py
A data.py
A requirements.txt
2
3
4
5
6
接下来我们执行git commit -m'first commit'
则会把暂村区的内容提交到本地的代码仓库。
blog git:(master) ✗ git commit -m'first commit'
[master (root-commit) 06284cd] first commit
5 files changed, 130 insertions(+)
create mode 100644 .gitignore
create mode 100644 app.py
create mode 100644 article.py
create mode 100644 data.py
create mode 100644 requirements.txt
(blog) ➜ blog git:(master) git status -s
(blog) ➜ blog git:(master)
2
3
4
5
6
7
8
9
10
最后
本章的内容就到这里了。我们来回顾一下,本章我们写了一个查询文章列表的接口。为这个接口写了一些模拟的数据。并且我们在写完代码之后,把它们放到了代码仓库中管理起来。
如果写出来运行不起来或者有啥疑问的同学,可以参考:代码浏览 - blog - 从零开始写一个博客系统 - svenweng (coding.net) 的代码