0Pricing
Flask Academy · レッスン

テンプレートにデータを渡す

Pythonの変数をレンダリングされたページに渡します

「テンプレートにデータを渡す」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Pages That Change

Static HTML is fine, but real pages show different data per user. You make pages dynamic by passing data from your view into the template.

Keyword Arguments

Add keyword arguments to render_template. Each name you pass becomes a variable you can use inside the HTML file.

return render_template('hi.html', name='Ada')

Reading a Variable

Inside the template, print a value with double curly braces. Flask swaps the expression for its result when the page renders.

<h1>Hello, {{ name }}!</h1>

The Expression Delimiter

Those {{ }} markers mean output this value here. Anything inside is evaluated and its result is written into the page.

Passing Many Values

You can pass as many keyword arguments as you need. Each one is independent and available throughout the template.

return render_template('p.html', name='Ada', age=36)

Passing Lists

Values are not limited to text. You can hand a template a list or any Python object and work with it in the markup.

return render_template('t.html', items=['a','b','c'])

Accessing Object Attributes

Use a dot to reach attributes, just like in Python. Templates read object properties the same intuitive way you do in code.

<p>{{ user.email }}</p>

Dictionary Lookups

For a dictionary, use either the dot or bracket form. Jinja2 tries attribute access first, then falls back to a key lookup.

{{ data.title }} or {{ data['title'] }}

Missing Values Are Safe

If a variable is undefined, Jinja2 renders nothing instead of crashing. That keeps a small typo from breaking the whole page.

Applying Filters

Transform a value on the fly with a filter, written after a pipe. Filters format text without cluttering your Python view.

<h1>{{ name|upper }}</h1>

Keep Logic in the View

Do heavy work in Python and pass ready values in. Templates should display data, not compute it, which keeps pages simple.

Quick Check

How do you output a variable named title inside a Jinja2 template?

Recap

You now pass data through keyword arguments and print it with double curly braces, even reaching attributes and applying filters. Great progress! 🚀

よくある質問

「テンプレートにデータを渡す」レッスンは無料ですか?

はい。「テンプレートにデータを渡す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「テンプレートにデータを渡す」で何を学びますか?

Pythonの変数をレンダリングされたページに渡します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「テンプレートにデータを渡す」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. render_templateとtemplatesフォルダー
  2. テンプレートにデータを渡す
  3. Jinja2のループと条件分岐
  4. 自動エスケープとsafeフィルター
← Flask Academyに戻る