0Pricing
Flask Academy · レッスン

文字列、HTML、ステータスコードを返す

さまざまな本文を送信し、HTTPステータスを設定します

「文字列、HTML、ステータスコードを返す」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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、ステータスコードを返す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「文字列、HTML、ステータスコードを返す」で何を学びますか?

さまざまな本文を送信し、HTTPステータスを設定します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「文字列、HTML、ステータスコードを返す」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. @app.routeデコレーター
  2. リクエストがレスポンスになるまで
  3. 文字列、HTML、ステータスコードを返す
  4. 1つのアプリに複数のルートを定義する
← Flask Academyに戻る