-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
39 lines (33 loc) · 1000 Bytes
/
app.py
File metadata and controls
39 lines (33 loc) · 1000 Bytes
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
from flask import Flask
from flask import render_template, request, redirect, url_for
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
time = db.Column(db.DateTime, nullable=False)
name = db.Column(db.String(120), nullable=False)
def __repr__(self):
return self
@app.route("/")
def home():
result = Todo.query.all()
return render_template(
"test.html",
entries = result,
)
@app.route("/add", methods=["POST"])
def add():
name = request.form['todo']
todo = Todo(time=datetime.now(), name=name)
db.session.add(todo)
db.session.commit()
return redirect(url_for('home'))
@app.route("/del/<id>")
def delete(id):
todo = Todo.query.filter_by(id=id).first()
db.session.delete(todo)
db.session.commit()
return redirect("/")