Redirect After Post (PRG Pattern)
Avoid duplicate submissions with a redirect.
Redirect After Post (PRG Pattern) is a free Flask Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎉
Frequently asked questions
Is the “Redirect After Post (PRG Pattern)” lesson free?
Yes — the full text of “Redirect After Post (PRG Pattern)” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Redirect After Post (PRG Pattern)”?
Avoid duplicate submissions with a redirect. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Redirect After Post (PRG Pattern)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Build an HTML Form That Posts
- Read and Default Query Parameters
- Validate Required Fields by Hand
- Redirect After Post (PRG Pattern)