-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
34 lines (28 loc) · 1.05 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
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
task_name = db.Column(db.String(200),nullable=False)
date_created = db.Column(db.DateTime,default=datetime.utcnow)
def __repr__(self):
return '<Tast %r>' % self.id
@app.route('/')
def index():
if request.method == "POST":
input_task_name = request.form['task-input']
new_todo_model = Todo(task_name=input_task_name)
try:
db.session.add(new_todo_model)
db.session.commit()
return redirect('/')
except:
return 'There was an error occured while creating new task'
else:
tasks = Todo.query.order_by(Todo.date_created).all()
return render_template('index.html',tasks=tasks)
if __name__ == "__main__":
app.run(debug=True)