-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (64 loc) · 2.45 KB
/
main.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
74
75
76
from flask import Flask, render_template,request,redirect
from flask_sqlalchemy import SQLAlchemy
from dotenv import load_dotenv
import os
load_dotenv()
db_url = os.environ["DATABASE_URL"]
db_url = db_url.replace('postgres', 'postgresql', 1)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] =db_url
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY']='thisisthesupersecretforthesecretkey'
db=SQLAlchemy(app)
class Todo(db.Model):
sno=db.Column(db.Integer,primary_key=True)
title=db.Column(db.String(200),nullable=False)
description = db.Column(db.String(200), nullable=False)
def __repr__(self) :
return "{} is the title and {} is the description".format(self.title,self.description)
#create all the tables and initialise them
db.create_all()
db.init_app(app)
@app.route("/",methods=['POST','GET'])
def hello_world():
if request.method=='POST':
title=request.form['title']
description=request.form['description']
todo=Todo(title=title ,description=description)
db.session.add(todo)
db.session.commit()
alltodos=Todo.query.all()
return render_template('index.html',todos=alltodos)
@app.route("/delete/<int:sno>")
def delete(sno):
deletetodo = Todo.query.filter_by(sno=sno).first()
db.session.delete(deletetodo)
db.session.commit()
return redirect("/")
@app.route("/update/<int:sno>",methods=['PATCH','POST','GET'])
def main_route(sno):
if request.method == 'POST':
updatetodo = Todo.query.filter_by(sno=sno).first()
title = request.form['title']
description = request.form['description']
updatetodo.title = title
updatetodo.description = description
db.session.commit()
return redirect("/")
else:
updatetodo = Todo.query.filter_by(sno=sno).first()
return render_template('update.html', updatetodo=updatetodo)
# @app.route("/updatetodo/<int:sno>",methods=['POST'])
# def update_todo(sno):
# if request.method == 'POST':
# updatetodo = Todo.query.filter_by(sno=sno).first()
# title = request.form['title']
# description = request.form['description']
# updatetodo.title=title
# updatetodo.description=description
# db.session.commit()
# return redirect("/")
# updatetodo = Todo.query.filter_by(sno=sno).first()
# return render_template('update.html', updatetodo=updatetodo)
if __name__ == "__main__":
app.run(debug=False)