0%

Python练习册:0024

题目

第 0024 题: 使用 Python 的 Web 框架,做一个 Web 版本 TodoList 应用。

0024

分析

这种综合项目涉及的知识点较多,这里我们用Flask1.0flask-sqlalchemy实现其逻辑,数据库使用SQLite3,用materialize样式美化我们的页面,jQuery优化用户体验。主要修改自李辉的Flask实践:待办事项,去除添加分类功能,只保留核心todo内容。

pip install flask flask-salalchemy

代码

  • 结构

templates/ -----> 模板 index.html
static/ -----> 静态页面 css js
app.py -----> flask 代码
todo.db -----> sqlite3数据库

1
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os

from flask import Flask
from flask import render_template, redirect, url_for, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

#配置数据库信息
DATABASE = "todo.db"
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////" + os.path.join(
basedir, DATABASE)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)


#数据库结构项
class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
category_id = db.Column(
db.Integer, db.ForeignKey('category.id'), default=1)


#构建数据库结构 类
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64))
#构造项与类的关系
items = db.relationship('Item', backref='category')


#初始化数据库,并添加测试数据
def init_db():
"""Insert default categories and demo items.
"""
db.create_all()
todo = Category(name=u'待完成')
done = Category(name=u'已完成')

item = Item(body=u'看一小时《战争与和平》')
item2 = Item(body=u'浇花', category=todo)
item3 = Item(body=u'收快递', category=done)

db.session.add_all([todo, done, item, item2, item3])
db.session.commit()


#首页
@app.route('/', methods=['GET', 'POST'])
def index():
#第一次运行时初始化数据库
if not os.path.exists(DATABASE): init_db()

if request.method == 'POST':
body = request.form.get('item')
category_id = request.form.get('category')
category = Category.query.get_or_404(category_id)
item = Item(body=body, category=category)
db.session.add(item)
db.session.commit()
return redirect(url_for('category', id=category_id))
return redirect(url_for('category', id=1))


#分类页
@app.route('/category/<int:id>')
def category(id):
category = Category.query.get_or_404(id)
categories = Category.query.all()
items = category.items
return render_template(
'index.html',
items=items,
categories=categories,
category_now=category)


#编辑某项
@app.route('/edit-item/<int:id>', methods=['GET', 'POST'])
def edit_item(id):
item = Item.query.get_or_404(id)
category = item.category
item.body = request.form.get('body')
db.session.add(item)
db.session.commit()
return redirect(url_for('category', id=category.id))


#添加到已完成
@app.route('/done/<int:id>', methods=['GET', 'POST'])
def done(id):
item = Item.query.get_or_404(id)
category = item.category
done_category = Category.query.get_or_404(2)
done_item = Item(body=item.body, category=done_category)
db.session.add(done_item)
db.session.delete(item)
db.session.commit()
return redirect(url_for('category', id=category.id))


#删除某项
@app.route('/delete-item/<int:id>')
def del_item(id):
item = Item.query.get_or_404(id)
category = item.category
if item is None:
return redirect(url_for('category', id=1))
db.session.delete(item)
db.session.commit()
return redirect(url_for('category', id=category.id))


if __name__ == '__main__':
app.run(debug=True)

效果

0024

参考

欢迎关注我的其它发布渠道