Branch Logic by request.method
Serve a form on GET and process it on POST.
Branch Logic by request.method is a free Flask Academy lesson on CoddyKit — lesson 2 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.
One Route, Two Jobs
A route that accepts both GET and POST often needs to behave differently for each. You handle that by checking which verb arrived.
The request Object
Flask gives you a global request object that describes the incoming call. Import it once and use it inside any view.
from flask import requestRead request.method
The attribute request.method holds the verb of the current call as a string like "GET" or "POST".
if request.method == "POST":
print("handling a form")Branch on the Verb
Wrap your logic in an if that checks request.method. One branch shows a form, the other processes the submitted data.
The Classic Form Pattern
The most common shape is: on GET render the empty form, and on POST read the answers and act on them.
@app.route("/contact", methods=["GET", "POST"])
def contact():
if request.method == "POST":
return "saved"
return "show form"GET Should Not Change Data
Keep the GET branch read-only. A GET request must never create or modify records, since browsers may repeat it freely.
POST Does the Work
Put writes, saves, and side effects in the POST branch. That keeps mutations tied to a deliberate submission.
Compare Strings Exactly
Always compare against the exact uppercase value. request.method is "POST", never "post", so a lowercase check will silently fail.
An else for the Fallthrough
Treat the GET case as the natural else. Return the form view when the method is not POST so the page always renders.
Skip Checks Flask Handles
You never test for a verb you did not allow. Flask blocks unlisted methods before your code runs, so only listed verbs arrive.
Keep Branches Small
If each branch grows large, call helper functions from the if. The view stays a clean dispatcher between GET and POST.
Quick Check
Pick the branch that should change data.
Recap
Use request.method to branch: render the form on GET, process and save on POST. Keep GET read-only and POST for changes. Great work! 🙌
Frequently asked questions
Is the “Branch Logic by request.method” lesson free?
Yes — the full text of “Branch Logic by request.method” 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 “Branch Logic by request.method”?
Serve a form on GET and process it on POST. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Branch Logic by request.method” 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
- Declare Allowed Methods on a Route
- Branch Logic by request.method
- Read POST Data from the Body
- 405 Method Not Allowed Explained