0Pricing
Flask Academy · レッスン

request.formでフォームフィールドを扱う

送信されたHTMLフォームから値を取り出します

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

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

Forms Send Data in the Body

When an HTML form posts, the field values travel inside the request body, not the URL, so request.args will not find them.

Meet request.form

For posted form fields Flask gives you request.form, a dict-like object keyed by each input name on the page.

name = request.form["name"]

The name Attribute Is the Key

A field shows up in request.form under its HTML name attribute, so name="email" becomes request.form["email"].

Use .get for Optional Fields

Just like with args, prefer .get so a field the user left out returns None instead of crashing your view with an error.

phone = request.form.get("phone")

Provide a Default

Give .get a fallback and missing optional fields take a clean default, keeping the rest of your logic simple.

role = request.form.get("role", "user")

Match the HTTP Method

Form data only arrives on a POST, so your route must allow POST or the browser gets a 405 before your code runs.

@app.route("/signup", methods=["POST"])

Form Values Are Strings Too

Every form value is text. Convert ages, prices, or counts yourself, and guard against bad input that will not parse.

age = int(request.form.get("age", 0))

Handle Multi-Value Fields

Checkboxes and multi-selects can send a key many times. Use getlist to collect every chosen value into a list.

picks = request.form.getlist("topics")

Form vs Args at a Glance

Remember the split: request.args reads the URL query string, while request.form reads fields from the posted body.

values Merges Both Sources

Need either source without caring which? request.values combines args and form into one dict-like view for convenience.

token = request.values.get("token")

A Simple Signup View

Putting it together, a signup handler reads name and email from the form and responds once it has them in hand.

@app.route("/signup", methods=["POST"])
def signup():
    name = request.form["name"]
    return f"Welcome {name}"

Quick Check

Check your grip on reading form fields.

Recap: Form Fields

You pulled posted fields from request.form, used .get with defaults, handled multi-value inputs, and saw how it differs from args. 🙌

よくある質問

「request.formでフォームフィールドを扱う」レッスンは無料ですか?

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

「request.formでフォームフィールドを扱う」で何を学びますか?

送信されたHTMLフォームから値を取り出します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「request.formでフォームフィールドを扱う」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. request.argsでクエリ文字列を扱う
  2. request.formでフォームフィールドを扱う
  3. request.get_jsonでJSON本文を扱う
  4. ヘッダー、Cookie、クライアントIP
← Flask Academyに戻る