Strings, HTML und Statuscodes zurückgeben
Senden Sie unterschiedliche Inhalte und setzen Sie den HTTP-Status.
Strings, HTML und Statuscodes zurückgeben ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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", 404Common 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", 201Common 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 = 202Strings 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. ✅
Häufig gestellte Fragen
Ist die Lektion „Strings, HTML und Statuscodes zurückgeben“ kostenlos?
Ja — der vollständige Text von „Strings, HTML und Statuscodes zurückgeben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Strings, HTML und Statuscodes zurückgeben“?
Senden Sie unterschiedliche Inhalte und setzen Sie den HTTP-Status. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Flask Academy zu starten?
Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Strings, HTML und Statuscodes zurückgeben“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?
Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Der Decorator @app.route
- Wie aus einer Anfrage eine Antwort wird
- Strings, HTML und Statuscodes zurückgeben
- Mehrere Routen in einer App