LoginSignup
1
0

More than 3 years have passed since last update.

FlaskのHTMLテンプレートの設定

Posted at

はじめに

ここでは、FlaskのHTMLテンプレートファイルの作成方法について解説します。

HTMLファイルにおける変数、関数の埋め込み

HTMLファイル内で、{{ 変数名 }}とすると、ビュー関数から渡された変数をHTMLファイル内に埋め込むことができます。
また、{% 関数名(引数1 引数2 ...) %}とすることで、関数を用いて表示内容を制御することもできます。

レコード一覧の表示

レコード一覧を{% for %}を用いて一つずつ取り出して、表示します。

app/samplemodel_list.html
{% for sample in table %}
    <h1>{{ sample[char_sample] }}</h1>
    <p>{{ sample[text_sample] }}</p>
{% endfor %}

個々のレコードの表示

個々のレコードはテーブル名[カラム名]とすることで表示することができます。

app/samplemodel_detail.html
<h1>{{ sample[char_sample] }}</h1>
<p>{{ sample[text_sample] }}</p>

まとめ

ここでは、FlaskのHTMLテンプレート作成の基本について解説しました。
次回は、テンプレートの継承やカスタムタグの作成について取り上げる予定です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0