-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (48 loc) · 1.75 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
# Import statements:
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
# Initialize the Flask application:
app = Flask(__name__)
# Configure the database to store list entries:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db'
# Instantiate SQLAlchemy:
db = SQLAlchemy(app)
# Create a class for the database model:
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(200))
complete = db.Column(db.Boolean)
# Create the database tables within the application context:
with app.app_context():
db.create_all()
# Create a route for "index.html":
@app.route('/')
def index():
incomplete = Todo.query.filter_by(complete=False).all()
complete = Todo.query.filter_by(complete=True).all()
return render_template('index.html', incomplete=incomplete,
complete=complete)
# Create a route for "add.html" which is where entries will be stored:
@app.route('/add', methods=['POST'])
def add():
todo = Todo(text=request.form['todoitem'], complete=False)
db.session.add(todo)
db.session.commit()
return redirect(url_for('index'))
# Create a route for "complete.html" to collect completed entries:
@app.route('/complete/<id>')
def complete(id):
todo = Todo.query.filter_by(id=int(id)).first()
todo.complete = True
db.session.commit()
return redirect(url_for('index'))
# Create a route for "remove.html" to delete completed entries:
@app.route('/remove/<id>')
def remove(id):
todo = Todo.query.filter_by(id=int(id)).first()
db.session.delete(todo)
db.session.commit()
return redirect(url_for('index'))
# Run the Flask application:
if __name__ == '__main__':
app.run(debug=True)