#完成画面
新規作成
app.py
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
task = request.form.get('task')
if task:
tasks.append(task)
return redirect(url_for('index'))
return render_template('add.html')
@app.route('/add_note', methods=['GET', 'POST'])
def add_note():
if request.method == 'POST':
note = request.form.get('note')
if note:
notes.append(note)
return redirect(url_for('index'))
return render_template('add_note.html')
@app.route('/add_list', methods=['GET', 'POST'])
def add_list():
if request.method == 'POST':
list = request.form.get('list')
if list:
lists.append(list)
return redirect(url_for('index'))
return render_template('add_list.html')
task / memo/優先度新規作成html
# task
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>優先度追加</title>
</head>
<body>
<h1>優先度追加</h1>
<form method="post">
<input type="text" name="list" required>
<button type="submit">追加</button>
</form>
<a href="{{ url_for('index') }}">戻る</a>
</body>
</html>
# memo
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>note追加</title>
</head>
<body>
<h1>メモ追加</h1>
<form method="post">
<input type="text" name="note" required>
<button type="submit">追加</button>
</form>
<a href="{{ url_for('index') }}">戻る</a>
</body>
</html>
# list
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>優先度追加</title>
</head>
<body>
<h1>優先度追加</h1>
<form method="post">
<input type="text" name="list" required>
<button type="submit">追加</button>
</form>
<a href="{{ url_for('index') }}">戻る</a>
</body>
</html>
一覧画面
app.py
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
tasks = []
notes = []
lists = []
@app.route('/')
def index():
return render_template('index.html', tasks=tasks, notes=notes, lists=lists)
html タスク メモ 優先度
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ToDoリスト</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1>ToDoリスト</h1>
<table>
<td><ul>
{% for task in tasks %}
<li>{{ task }} <a href="{{ url_for('edit_task', index=loop.index0) }}">編集</a>
<a href="{{ url_for('delete', task_id=loop.index0) }}">削除</a></li>
{% endfor %}
</ul>
<a href="{{ url_for('add') }}">タスクを追加</a>
</td>
</table>
<ul>
<h1>メモリスト</h1>
{% for note in notes %}
<li>{{ note }} <a href="{{ url_for('edit_note', index=loop.index0) }}">編集</a>
<a href="{{ url_for('delete_note', note_id=loop.index0) }}">削除</a></li>
{% endfor %}
</ul>
<ul>
<a href="{{ url_for('add_note') }}">メモを追加</a>
</ul>
<h1>優先度リスト</h1>
<ul>
{% for list in lists %}
<li>{{ list }}<a href="{{ url_for('edit_list', index=loop.index0) }}">編集</a></li>
<a href="{{ url_for('delete_list', list_id=loop.index0) }}">削除</a></li>
{% endfor %}
</ul>
</ul>
<ul>
<a href="{{ url_for('add_list') }}">優先度を追加</a>
</ul>
</body>
</html>
編集画面
app.py
@app.route('/edit_task/<int:index>', methods=['GET', 'POST'])
def edit_task(index):
if index < len(tasks):
if request.method == 'POST':
new_task = request.form.get('task')
if new_task:
tasks[index] = new_task
return redirect(url_for('index'))
return render_template('edit_task.html', task=tasks[index])
else:
return "タスクが見つかりません" # インデックスが範囲外の場合
@app.route('/edit_note/<int:index>', methods=['GET', 'POST'])
def edit_note(index):
if index < len(notes):
if request.method == 'POST':
new_note = request.form.get('task')
if new_note:
notes[index] = new_note
return redirect(url_for('index'))
return render_template('edit_note.html', note=notes[index])
else:
return "noteめよら"
@app.route('/edit_list/<int:index>', methods=['GET', 'POST'])
def edit_list(index):
if index < len(lists):
if request.method == 'POST':
new_list = request.form.get('list')
if new_list:
lists[index] = new_list
return redirect(url_for('index'))
return render_template('edit_list.html', list=lists[index])
else:
return "listめよら"
編集html タスク/メモ/優先度
# タスク
<form method="POST">
<input type="text" name="task" value="{{ task }}">
<button type="submit">保存</button>
</form>
# メモ
<form method="POST">
<input type="text" name="note" value="{{ note }}">
<button type="submit">保存</button>
</form>
# list
<form method="POST">
<input type="text" name="list" value="{{ list }}">
<button type="submit">保存</button>
</form>
削除
app.py
@app.route('/delete/<int:task_id>')
def delete(task_id):
if 0 <= task_id < len(tasks):
tasks.pop(task_id)
return redirect(url_for('index'))
@app.route('/delete/<int:note_id>')
def delete_note(note_id):
if 0 <= note_id < len(notes):
notes.pop(note_id)
return redirect(url_for('index'))
@app.route('/delete/<int:list_id>')
def delete_list(lisk_id):
if 0 <= list_id < len(lists):
lists.pop(list_id)
return redirect(url_for('index'))
心臓 run.py中身
from app.app import app
if __name__ == "__main__":
app.run(debug=True, use_reloader=True)