Created
December 16, 2025 02:12
-
-
Save smzn/d037d58ec72a467acb1516354121408f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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には入っていない) | |
| new_location = Location(name=name, latitude=lat, longitude=lng) | |
| # 3. DBセッションに追加してコミット (ここでDBに保存される) | |
| db.session.add(new_location) | |
| db.session.commit() | |
| # 4. 一覧画面へリダイレクト (再読み込み時の二重送信防止のため) | |
| return redirect(url_for('index')) | |
| # 普通にURLにアクセスしたとき (GETメソッド) は、登録フォームを表示 | |
| return render_template('create.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment