Skip to Content
ファストAPI
book

ファストAPI

by Bill Lubanovic
March 2025
Intermediate to advanced
280 pages
3h 20m
Japanese
O'Reilly Media, Inc.
Content preview from ファストAPI

第16章 フォームとテンプレート フォームとテンプレート

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

プレビュー

FastAPIAPIはその主な焦点のヒントですが、FastAPI は従来の Web コンテンツも処理できます。 この章では、HTML にデータを挿入するための標準 HTML フォームとテンプレートについて説明します。

フォーム

これまで見てきたように、FastAPIは主にAPIを構築するために設計されており、そのデフォルトの入力はJSONである。 しかし、だからといって標準的なバナナHTMLやフォーム、友達を提供できないというわけではない。

FastAPI は、QueryPath のような他のソースと同様に、Form 依存関係を使用して HTML フォームからのデータをサポートする。

FastAPIフォームを実行するには、Python-Multipartパッケージが必要なので、必要ならpip install python-multipart 。 また、この章のテストフォームを格納するために、第15章の静的ディレクトリが必要になる。

例3-11をやり直すが、JSON文字列の代わりにフォームでwho 。 (このパス関数greet2() を呼び出し、古いパス関数greet() がまだ残っていれば、それを使わないようにする。)main.pyに 例16-1を追加する。

例16-1. GET フォームから値を取得する
from fastapi import FastAPI, Form

app = FastAPI()

@app.get("/who2")
def greet2(name: str = Form()):
    return f"Hello, {name}?"

主な違いは、PathQuery第3章のその他の代わりにForm

例16-2のHTTPieを使った初期フォームテストを試してみよう(-f 、JSONとしてではなくフォームエンコーディングでアップロードする必要がある)。

例16-2. HTTPieを使ったフォームGET リクエスト
$ http -f -b GET localhost:8000/who2 name="Bob Frapples"
"Hello, Bob Frapples?"

例16-3では、このファイル(form1.html)をそこに置くことにする。

例16-3. フォームGET リクエスト(static/form1.html)
<form action="http://localhost:8000/who2" method="get">
Say hello to my little friend:
<input type="text" name="name" value="Bob Frapples">
<input type="submit">
</form>

ブラウザにhttp://localhost:8000/static/form1.html、フォームが表示される。任意のテスト文字列を入力すると、このような結果が返ってくる:

"detail":[{"loc":["body","name"],
           "msg":"field required",
           "type":"value_error.missing"}]}

え?

Uvicornが起動しているWindowsでログを見る:

INFO: 127.0.0.1:63502 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GitHubアクションを学ぶ

GitHubアクションを学ぶ

Brent Laster
仕事ではじめる機械学習 第2版

仕事ではじめる機械学習 第2版

有賀 康顕, 中山 心太, 西林 孝

Publisher Resources

ISBN: 9798341624658