タイトル: テンプレートの作成と共通化
SEOタイトル: djangoのテンプレートの作成とヘッダー・フッターの共通化
この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。
<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
<thead>
<tr><th style="background-color:#f39c12;color:#ffffff;text-align:left">この記事の要点</th></tr>
</thead>
<tbody>
<tr>
<td style="background-color:#fef9e7">
<ul>
<li>Django の<strong>テンプレート(templates フォルダ)</strong>作成と共通化方法</li>
<li>基本: アプリディレクトリ直下に <code>templates/</code> を作成 → <code>index.html</code> 等を配置</li>
<li><strong>共通化は <code>{% extends %}</code> + <code>{% block %}</code></strong> でレイアウト継承</li>
<li>ヘッダ/フッタを共通化することで保守性が大きく上がる</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p> </p>
<h2>テンプレートの基本</h2>
<p>アプリケーションディレクトリ直下に<strong>templates</strong>ディレクトリを作成しましょう。</p>
<p>templatesディレクトリ内に以下の<strong>index.html</strong>を作成します。</p>
<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
<tbody>
<tr>
<td style="background-color:#000000">
<p><span style="color:#ffffff">{% </span><span style="color:#f1c40f">if </span><span style="color:#ffffff">item_list %}</span></p>
<p><span style="color:#ffffff"> <ul></span></p>
<p><span style="color:#ffffff"> {% </span><span style="color:#f1c40f">for </span><span style="color:#ffffff">item </span><span style="color:#f1c40f">in </span><span style="color:#ffffff">item_list %}</span></p>
<p><span style="color:#ffffff"> <li>{{ item }}</li></span></p>
<p><span style="color:#ffffff"> {% </span><span style="color:#f1c40f">endfor </span><span style="color:#ffffff">%}</span></p>
<p><span style="color:#ffffff"> </ul></span></p>
<p><span style="color:#ffffff">{% </span><span style="color:#f1c40f">else </span><span style="color:#ffffff">%}</span></p>
<p><span style="color:#ffffff"> <p>No item.</p></span></p>
<p><span style="color:#ffffff">{% </span><span style="color:#f1c40f">endif </span><span style="color:#ffffff">%}</span></p>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p>templateに値を渡すviewを作成しましょう。</p>
<p>アプリケーションディレクトリ直下の<strong>views.py</strong>に以下の記述をしましょう。</p>
<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
<tbody>
<tr>
<td style="background-color:#000000">
<p><span style="color:#ffffff">from django.shortcuts import render</span></p>
<p><span style="color:#ffffff">def index(</span><span style="color:#f1c40f">request</span><span style="color:#ffffff">):</span></p>
<p><span style="color:#ffffff"> item_list = ['a', 'b', 'c']</span></p>
<p><span style="color:#f1c40f"> template = loader.get_template('index.html')</span></p>
<p><span style="color:#ffffff"> </span><span style="color:#f1c40f">context</span><span style="color:#ffffff"> = {</span></p>
<p><span style="color:#ffffff"> 'item_list': item_list,</span></p>
<p><span style="color:#ffffff"> }</span></p>
<p><span style="color:#ffffff"> return </span><span style="color:#f1c40f">render(request, 'index.html', context)</span></p>
</td>
</tr>
</tbody>
</table>
<p>indexにリクエストするとテンプレートを表示することが出来ます。</p>
<p> </p>
<h2>テンプレートの共通化</h2>
<p>ヘッダーやフッターといったテンプレートの共通部分を実装するには以下のようにします。</p>
<p>以下、共通部分となる<strong>base.html</strong>です。</p>
<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
<tbody>
<tr>
<td style="background-color:#000000">
<p><span style="color:#ffffff"><!DOCTYPE html></span></p>
<p><span style="color:#ffffff"><html lang="ja"></span></p>
<p><span style="color:#ffffff"> <head></span></p>
<p><span style="color:#ffffff"> <meta charset="UTF-8"></span></p>
<p><span style="color:#ffffff"> <title></span><span style="color:#f1c40f">{% block title %}{% endblock title %}</span><span style="color:#ffffff"></title></span></p>
<p><span style="color:#ffffff"> </head></span></p>
<p><span style="color:#ffffff"> <body></span></p>
<p><span style="color:#f1c40f"> {% block content %}</span></p>
<p><span style="color:#f1c40f"> {% endblock %}</span></p>
<p><span style="color:#ffffff"> </body></span></p>
<p><span style="color:#ffffff"></html></span></p>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p>独自実装の部分は</p>
<p><strong><span style="color:#ffffff"> </span><span style="color:#f1c40f">{% block content %}</span></strong></p>
<p><strong><span style="color:#ffffff"> </span><span style="color:#f1c40f">{% endblock %}</span></strong></p>
<p>となります。</p>
<p><strong><span style="color:#f1c40f">{% block title %}{% endblock title %}</span></strong></p>
<p>は後述します。</p>
<p> </p>
<p>以下、独自実装部分のテンプレートです。</p>
<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
<tbody>
<tr>
<td style="background-color:#000000">
<p><span style="color:#f1c40f">{% extends "base.html" %}</span></p>
<p><span style="color:#f1c40f">{% block title %}</span><span style="color:#ffffff">ページタイトル</span><span style="color:#f1c40f">{% endblock title %}</span></p>
<p><span style="color:#f1c40f">{% block content %}</span></p>
<p><span style="color:#ffffff"> <form method="post"></span></p>
<p><span style="color:#ffffff"> {% csrf_token %}</span></p>
<p><span style="color:#ffffff"> ...</span></p>
<p><span style="color:#ffffff"> </form></span></p>
<p><span style="color:#f1c40f">{% endblock %}</span></p>
</td>
</tr>
</tbody>
</table>
<p><span style="color:#e74c3c"><strong>extends</strong></span>で共通テンプレートを読み込んで<span style="color:#e74c3c"><strong>block content</strong></span>内に独自実装部分を記載します。</p>
<p><span style="color:null">ページごとにタイトルを変える必要があるので、</span><span style="color:#e74c3c"><strong>block title</strong></span><span style="color:null">に対してページのタイトルを設定しましょう。</span></p>
<p> </p>
<p><span style="color:#e74c3c"><strong>include</strong></span>を使用することで個別にテンプレートを読み込むことが出来ます。</p>
<p>例えば以下の様にヘッドタグを部品化しましょう。</p>
<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
<tbody>
<tr>
<td style="background-color:#000000">
<p><span style="color:#ffffff"><head></span></p>
<p><span style="color:#ffffff"> </span><span style="color:#f1c40f">{% include "headtag.html" %}</span></p>
<p><span style="color:#ffffff"> <title>{% block title %}{% endblock title %}</title></span></p>
<p><span style="color:#ffffff"></head></span></p>
</td>
</tr>
</tbody>
</table>
<p>以下、headtag.htmlの記載例。</p>
<table border="1" cellpadding="1" cellspacing="1" style="width:1000px">
<tbody>
<tr>
<td style="background-color:#000000">
<p><span style="color:#ffffff">{% load static %}</span></p>
<p><span style="color:#ffffff"><meta charset="UTF-8"></span></p>
<p><span style="color:#ffffff"><!-- CSS --><br />
<link href="{% static 'css/app.css' %}" rel="stylesheet"></span></p>
<p><span style="color:#ffffff"><!-- Scripts --><br />
<script type="text/javascript" src="{% static 'js/jquery-3.2.1.min.js' %}"></script></span><br />
</p>
</td>
</tr>
</tbody>
</table>
<p> </p>