0Pricing
Flask Academy · レッスン

POST後にリダイレクトする(PRGパターン)

リダイレクトで重複送信を防ぎます

「POST後にリダイレクトする(PRGパターン)」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Double Submit Problem

After a form post, hitting refresh resends the same data. That can create duplicate orders, comments, or signups. 😬

Why Refresh Repeats

The browser remembers the last request. Reloading a page that was the result of a POST simply fires that POST again.

Meet Post Redirect Get

The fix is the PRG pattern: accept the POST, then redirect the browser to a plain GET page instead of rendering directly.

Process the POST

First do the real work inside the POST branch: save the record, charge the card, whatever the form was for.

if request.method == "POST":
    save_comment(request.form["text"])

Redirect, Do Not Render

Instead of returning a template, return a redirect. The browser then makes a fresh GET that is safe to refresh.

from flask import redirect
return redirect("/thanks")

Build the Target With url_for

Avoid hardcoded paths. Use url_for so the redirect target follows your route names even if URLs change later.

return redirect(url_for("thanks"))

The 302 Status

A redirect sends a 302 response telling the browser to go fetch another URL. That second request is the safe GET.

The GET Handler

The target route is an ordinary GET view. It just shows a confirmation page and can be refreshed all day with no side effects.

@app.route("/thanks")
def thanks():
    return render_template("thanks.html")

Carry a Flash Message

Want to say it worked? Set a one time flash message before redirecting and show it on the destination page.

from flask import flash
flash("Saved!")
return redirect(url_for("thanks"))

Redirect on Errors Too

If validation fails, you can flash the problem and redirect back to the form, keeping every path refresh safe.

if errors:
    flash("Fix the form")
    return redirect(url_for("signup"))

The Full PRG View

Process, then redirect. This PRG shape is the standard for any form that changes data on the server.

@app.route("/comment", methods=["POST"])
def comment():
    save_comment(request.form["text"])
    return redirect(url_for("thanks"))

Quick Check

Why does PRG stop those accidental duplicates?

Recap: PRG Pattern

You processed the POST, redirected to a GET with url_for, used flash for messages, and made every refresh safe. 🎉

よくある質問

「POST後にリダイレクトする(PRGパターン)」レッスンは無料ですか?

はい。「POST後にリダイレクトする(PRGパターン)」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「POST後にリダイレクトする(PRGパターン)」で何を学びますか?

リダイレクトで重複送信を防ぎます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「POST後にリダイレクトする(PRGパターン)」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. POSTするHTMLフォームを構築する
  2. クエリパラメーターを読み取りデフォルト値を設定する
  3. 必須フィールドを手動で検証する
  4. POST後にリダイレクトする(PRGパターン)
← Flask Academyに戻る