request.methodで処理を分岐する
GETではフォームを表示し、POSTでは処理します
「request.methodで処理を分岐する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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! 🙌
よくある質問
「request.methodで処理を分岐する」レッスンは無料ですか?
はい。「request.methodで処理を分岐する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「request.methodで処理を分岐する」で何を学びますか?
GETではフォームを表示し、POSTでは処理します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「request.methodで処理を分岐する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ルートで許可するメソッドを宣言する
- request.methodで処理を分岐する
- 本文からPOSTデータを読み取る
- 405 Method Not Allowedを解説