The python version you are using is 3.6 It's a memorandum of my own, but I'd be happy if you could give me some advice.
todo.py
#coding:utf-8
from bottle import run,route,template,redirect,request,post
import sqlite3
@route("/")
def index():
todo_list = get_todo()
return template("index",todo_list=todo_list)
@route("/enter",method=["POST"])
def enter():
todo=request.POST.getunicode("todo_list")
#Todo to database_write list
save_todo(todo)
#return todo
return redirect("/")
@route("/delete",method=["POST"])
def delete():
conn=sqlite3.connect("todo.db")
c=conn.cursor()
delete="delete from todo_list where todo='{0}'".format(request.POST.getunicode("finished"))
c.execute(delete)
conn.commit()
return redirect("/")
#Todo to database_save list
def save_todo(todo):
conn= sqlite3.connect('todo.db')
c= conn.cursor()
insert="insert into todo_list(todo) values('{0}')".format(todo)
c.execute(insert)
conn.commit()
#Database todo_read list
def get_todo():
conn= sqlite3.connect('todo.db')
c= conn.cursor()
select="select * from todo_list"
c.execute(select)
row = c.fetchall()
return row
run(host="localhost",port=8000,debug=True,reloader=True)
index.html
<!DOCTYPE html>
<html lang="jp">
<head>
<meta charset="UTF-8">
<title>To Do App</title>
</head>
<body>
<h1>Welcome to ToDo list</h1>
<form name="todo" method="POST" action="/enter">
<input type="text" name="todo_list" required /><br/>
<input type="submit" value="add to" />
</form>
<form method="POST" action="/delete">
% for todo in todo_list:
<input type="checkbox" name="finished"value="{{todo[0]}}">{{todo[0]}}<br>
% end
<input type="submit" value="Clear checked list">
</form>
</body>
</html>
It was my first time to create a web application, so I had a lot of trouble. Also, since the degree of completion is still low, I will continue to improve it. In particular, I haven't done any design, so I hope I can make it look cool.
Recommended Posts