Skip to content

Instantly share code, notes, and snippets.

<td>
<a href="{{ url_for('update', id=location.id) }}">編集</a>
| <form action="{{ url_for('delete', id=location.id) }}" method="POST" style="display:inline;">
<button type="submit" onclick="return confirm('本当に削除しますか?');">削除</button>
</form>
</td>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>編集</title>
</head>
<body>
<h1>場所の編集</h1>
<form method="POST">
@app.route('/update/<int:id>', methods=['GET', 'POST'])
def update(id):
# 1. 編集したいデータをDBから取得
location = Location.query.get_or_404(id)
# 2. ポスト送信されたら(保存ボタンが押されたら)
if request.method == 'POST':
# フォームのデータで上書きする
location.name = request.form.get('name')
location.latitude = request.form.get('latitude')
<td>
<form action="{{ url_for('delete', id=location.id) }}" method="POST" style="display:inline;">
<button type="submit" onclick="return confirm('本当に削除しますか?');">削除</button>
</form>
</td>
# <int:id> は「URLのこの部分を整数の id という変数で受け取る」という意味です
@app.route('/delete/<int:id>', methods=['POST'])
def delete(id):
# 1. 指定されたIDのデータを探す (なければ404エラーを出す便利機能)
location = Location.query.get_or_404(id)
# 2. 削除処理をステージング
db.session.delete(location)
# 3. コミット (DBに反映)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>新規登録</title>
</head>
<body>
<h1>場所の新規登録</h1>
<form method="POST">
@app.route('/create', methods=['GET', 'POST'])
def create():
# フォームの送信ボタンが押されたとき (POSTメソッド)
if request.method == 'POST':
# 1. フォームの入力値を取得 (HTMLの name属性 を指定)
name = request.form.get('name')
lat = request.form.get('latitude')
lng = request.form.get('longitude')
# 2. モデルのインスタンスを作成 (まだDBには入っていない)
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>「{{ category.name }}」のアプリ一覧</title>
</head>
<body>
<p>
<a href="{{ url_for('index') }}">← トップページ(すべて表示)に戻る</a>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>製品アプリケーション一覧</title>
</head>
<body>
<h2>カテゴリから探す</h2>
<ul>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>{{ application.title }} - 詳細</title>
</head>
<body>
<p>
<a href="{{ url_for('index') }}">← アプリ一覧に戻る</a>