-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
75 lines (57 loc) · 1.9 KB
/
app.py
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
import os
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask import Flask, request, redirect, render_template
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///logs.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Init SQLAlchemy
db = SQLAlchemy(app)
# Log data model
class Log(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return f'<Log {self.id}>'
@app.route('/')
def index():
return 'Welcome to the Work Log!'
@app.route('/create', methods=['GET', 'POST'])
def create():
if request.method == 'POST':
# 处理表单提交的数据
title = request.form['title']
content = request.form['content']
# 将日志保存到数据库中
log = Log(title=title, content=content)
db.session.add(log)
db.session.commit()
return redirect('/')
else:
# 显示表单
return '''
<form method="post">
<label for="title">Title</label>
<input type="text" name="title" id="title" />
<label for="content">Content</label>
<textarea name="content" id="content"></textarea>
<input type="submit" value="Create" />
</form>
'''
@app.route('/logs')
def logs():
logs = Log.query.all()
return render_template('logs.html', logs=logs)
@app.route('/logs/<int:id>')
def log(id):
log = Log.query.get_or_404(id)
return render_template('log.html', log=log)
if __name__ == '__main__':
if not os.path.exists('logs.db'):
open('logs.db', 'w').close()
with app.app_context():
model = Log
db.create_all()
app.run(debug=True)