0Pricing
Flask Academy · 课时

返回字符串、HTML 与状态码

发送不同的正文并设置 HTTP 状态

返回字符串、HTML 与状态码 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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. ✅

常见问题解答

「返回字符串、HTML 与状态码」课时是免费的吗?

是的 — 「返回字符串、HTML 与状态码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「返回字符串、HTML 与状态码」这节课中我会学到什么?

发送不同的正文并设置 HTTP 状态 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「返回字符串、HTML 与状态码」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. @app.route 装饰器
  2. 请求如何变成响应
  3. 返回字符串、HTML 与状态码
  4. 一个应用中的多个路由
← 返回 Flask Academy