request.argsでクエリ文字列を扱う
疑問符以降のURLパラメーターを読み取ります
「request.argsでクエリ文字列を扱う」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What Is a Query String
A query string is the part of a URL after the question mark, like ?q=cats&page=2. It carries small bits of optional input. 🔎
Where request Comes From
Flask gives you a global request object that holds everything about the incoming call. Import it once and reach for it inside any view.
from flask import requestMeet request.args
The request.args attribute is a dict-like object holding every key and value parsed from the query string for you.
Read a Value by Key
Treat request.args like a dictionary: index it by the parameter name to pull the matching value straight out of the URL.
term = request.args["q"]Prefer .get to Avoid Crashes
Indexing a missing key raises an error. Use .get instead so an absent parameter returns None rather than a 400.
term = request.args.get("q")Supply a Default Value
Pass a second argument to .get and that value is used when the key is missing, keeping your view safe and predictable.
page = request.args.get("page", "1")Everything Arrives as a String
Query values are always text, even numbers. If you need an int, convert it yourself after reading the raw string.
page = int(request.args.get("page", 1))Coerce With the type Argument
The type keyword on .get converts the value for you and falls back to the default if conversion fails.
page = request.args.get("page", 1, type=int)Repeated Keys With getlist
When one key appears several times, like ?tag=a&tag=b, use getlist to receive every value as a Python list.
tags = request.args.getlist("tag")Loop Over All Parameters
Because request.args behaves like a dict, you can iterate its items to inspect or log every parameter a client sent.
for k, v in request.args.items():
print(k, v)A Realistic Search View
Combine these pieces and your search endpoint reads a term and a page cleanly, with sensible defaults baked in.
@app.route("/search")
def search():
q = request.args.get("q", "")
return f"Searching {q}"Quick Check
Time to lock in how query strings work.
Recap: Query Strings
You read URL parameters through request.args, used .get with defaults, coerced types, and grabbed repeated keys with getlist. Nicely done! 🎉
よくある質問
「request.argsでクエリ文字列を扱う」レッスンは無料ですか?
はい。「request.argsでクエリ文字列を扱う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「request.argsでクエリ文字列を扱う」で何を学びますか?
疑問符以降のURLパラメーターを読み取ります ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「request.argsでクエリ文字列を扱う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- request.argsでクエリ文字列を扱う
- request.formでフォームフィールドを扱う
- request.get_jsonでJSON本文を扱う
- ヘッダー、Cookie、クライアントIP