0Pricing
Flask Academy · Lezione

Restituire stringhe, HTML e codici di stato

Invii corpi diversi e imposti lo stato HTTP.

Restituire stringhe, HTML e codici di stato è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 3 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.

Returning a Plain String

The simplest response is a plain string. Flask sends it straight to the browser as the page body with a 200 status.

@app.route("/")
def home():
    return "Hello"

Strings Can Hold HTML

Your string can contain real HTML tags. The browser parses them, so returning a heading renders as a styled title, not as raw text.

return "<h1>Welcome</h1>"

Inline HTML Gets Messy

Writing big pages as one giant string gets unreadable fast. It works for tiny snippets, but real pages belong in template files later.

The Default Status

When you return just a body, Flask assumes a 200 status, meaning OK. You only set a code when the result is not a plain success.

Return a Status Code

Return a tuple of body and number to set the status. Here a missing item answers with a 404 instead of the default 200.

return "Not found", 404

Common Success Codes

Use 201 when you create something new and 200 for a normal read. The right code tells clients exactly what happened.

return "Created", 201

Common Error Codes

Use 400 for bad input, 404 for missing pages, and 500 when your own code crashes. Each number has a clear, agreed meaning.

Add Headers Too

Extend the tuple with a third part, a dict of headers, when you need to control things like the content type of the reply.

return "OK", 200, {"X-App": "demo"}

make_response for Control

For finer control, build a make_response object. You can set its status and headers step by step before returning it.

resp = make_response("Hi")
resp.status_code = 202

Strings Are Auto-Wrapped

Even a bare string gets wrapped in a full response behind the scenes. Flask handles the boilerplate so your views stay short.

Pick the Honest Code

Always return a truthful status. Sending 200 for an error confuses clients and hides real bugs from anyone using your app.

Quick Check

Pick the response that sets a custom status.

Recap

You can return plain text, HTML, or a tuple with a status and headers. Honest status codes keep your responses clear and trustworthy. ✅

Domande Frequenti

La lezione «Restituire stringhe, HTML e codici di stato» è gratuita?

Sì — il testo completo di «Restituire stringhe, HTML e codici di stato» è 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 «Restituire stringhe, HTML e codici di stato»?

Invii corpi diversi e imposti lo stato HTTP. 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 3 di 4.

Quanto tempo richiede la lezione «Restituire stringhe, HTML e codici di stato»?

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

  1. Il decoratore @app.route
  2. Come una richiesta diventa una risposta
  3. Restituire stringhe, HTML e codici di stato
  4. Più route in una sola app
← Torna a Flask Academy