-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (44 loc) · 1.82 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
from flask import Flask, render_template, request, redirect, session
app = Flask(__name__)
app.secret_key = 'your_secret_key'
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
title = request.form['title']
desc = request.form['desc']
if len(title) != 0:
# If the 'todos' list does not exist in session, initialize it
if 'todos' not in session:
session['todos'] = []
# Add a new todo to the session
session['todos'].append({'title': title, 'desc': desc})
session.modified = True # Mark the session as modified so it will be saved
return redirect('/')
# Get the todos from session
allTodo = session.get('todos', [])
return render_template('index.html', allTodo=allTodo)
@app.route('/update/<int:index>', methods=['GET', 'POST'])
def update(index):
if 'todos' not in session or len(session['todos']) <= index:
return redirect('/')
if request.method == 'POST':
title = request.form['title']
desc = request.form['desc']
if len(title) != 0:
# Update the todo at the specified index
session['todos'][index]['title'] = title
session['todos'][index]['desc'] = desc
session.modified = True # Mark the session as modified
return redirect('/')
todo = session['todos'][index]
return render_template('update.html', todo=todo, index=index)
@app.route('/delete/<int:index>')
def delete(index):
if 'todos' not in session or len(session['todos']) <= index:
return redirect('/')
# Remove the todo at the specified index
del session['todos'][index]
session.modified = True # Mark the session as modified
return redirect('/')
if __name__ == '__main__':
app.run(debug=False)