0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

todoアプリ 解説

Last updated at Posted at 2025-04-25

登場人物一覧

 登録したいもの : task

 登録する時一時的に使うもん :tasks
 
 削除時使うもん :task_id

task新規登録したら、一覧に戻る

app.py
if request.method == 'POST':
        task = request.form.get('task')
        if task:
            tasks.append(task)
        return redirect(url_for('index'))

”タスクを追加”リンクをクリックしたら、add.index(タスク追加画面)へ

index.html
<a href="{{ url_for('add') }}">タスクを追加</a>

”削除”リンクをクリックしたら、削除され一覧画面(index.html)へ

index.html
<a href="{{ url_for('delete', task_id=loop.index0) }}">削除</a>

削除処理

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'))

戻るボタンを押したら、index.html(タスク一覧画面)へ

index.html
<a href="{{ url_for('index') }}">戻る</a>

登録したいものが「タスク」と「メモ」の2つに

###一覧

app.py
from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

tasks = []
notes = []  # メモ用のリスト

@app.route('/')
def index():
    return render_template('index.html', tasks=tasks, notes=notes)

task追加リンククリックしたら、タスク追加画面へ

<form method="POST" action="{{ url_for('add_task') }}">
  <input type="text" name="task" placeholder="タスクを入力">
  <button type="submit">タスク追加</button>
</form>

メモ追加リンククリックしたら、メモ追加画面へ

<form method="POST" action="{{ url_for('add_note') }}">
  <input type="text" name="note" placeholder="メモを入力">
  <button type="submit">メモ追加</button>
</form>

一覧画面

<h3>メモ一覧</h3>
<ul>
  {% for note in notes %}
    <li>{{ note }}</li>
  {% endfor %}
</ul>

task追加処理とメモ追加処理

@app.route('/add_task', methods=['POST'])
def add_task():
    task = request.form.get('task')
    if task:
        tasks.append(task)
    return redirect(url_for('index'))

@app.route('/add_note', methods=['POST'])
def add_note():
    note = request.form.get('note')
    if note:
        notes.append(note)
    return redirect(url_for('index'))

データ登録したから一覧画面に戻る

 redirect

編集機能

データを新しく上書きし、一覧画面へ

app.py
if request.method == 'POST':
        new_task = request.form.get('task')
        tasks[index] = new_task
        return redirect(url_for('index'))

編集ボタン表示

<a href="{{ url_for('edit', index=loop.index0) }}">編集</a>

編集画面作成

<form method="POST">
  <input type="text" name="task" value="{{ task }}">
  <button type="submit">保存</button>
</form>

タスクを編集する時

IndexError: list assignment index out of range

ifでdataが長いか短いかチェックしてない<```

スクリーンショット 2025-04-25 18.37.08.png

解決策
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 "タスクが見つかりません"  # インデックスが範囲外の場合

edit 画面 if index < len(tasks): 仕組み

ないデータ( tasks[99]ちょーだい )リクエストがきたらそんなインデックスないって怒る仕組みになってる
だからif index < len(tasks):ってチェック

$$$ ファイル変更あったら自動で再リロード

run.py か app.py
if __name__ == "__main__":
    app.run(debug=True, use_reloader=True)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?