2
2

More than 3 years have passed since last update.

DjangoのHTMLテンプレートその2

Posted at

はじめに

ここでは、djangoのHTMLテンプレートの継承などについて解説します。

HTMLテンプレートの継承

extendsを用いることで、ベースになるHTMLテンプレートファイルを継承することができます。
{% block ブロック名 %}とすることで書き換えるブロックを作ることができます。CSSやjavascriptのカスタムファイルの読み込みにも使えます。

base.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ページタイトル</title>
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>

sample.html
{% extends 'base.html' %}

{% block content %}

{% endblock content %}

テンプレートの組み込み

includesを用いることで、HTMLファイルをページの一部として組み込むことができます。

sidebar.html
<div class="sidebar">
<p>サイドバー</p>
</div>
sample.html
{% extends 'base.html' %}

{% block content %}
<div class="main-content">
<p>メインコンテンツ</p>
</div>
{% includes 'sidebar.html' %}
{% endblock content %}

まとめ

ここでは、djangoのHTMLテンプレートの応用について解説しました。
今後も随時内容を追加していきます。

2
2
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
2
2