Query string tramite request.args
Legga i parametri URL dopo il punto interrogativo.
Query string tramite request.args è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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! 🎉
Domande Frequenti
La lezione «Query string tramite request.args» è gratuita?
Sì — il testo completo di «Query string tramite request.args» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.
Cosa imparerò in «Query string tramite request.args»?
Legga i parametri URL dopo il punto interrogativo. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Flask Academy?
Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Query string tramite request.args»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Flask Academy?
Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Query string tramite request.args
- Campi del form tramite request.form
- Body JSON tramite request.get_json
- Header, cookie e IP del client