7. Extends a template
<!-- home.html -->
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
<div class="post-wrapper">
<div class="post">
…
</div>
</div>
{% endfor %}
{% endblock content %}
8. Extends a template
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
<div class="post-wrapper">
<div class="post">
…
</div>
</div>
{% endfor %}
{% endblock content %}
{% extends %} must be the first template tag
9. Fill the Block
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
<div class="post-wrapper">
<div class="post">
…
</div>
</div>
{% endfor %}
{% endblock content %}
base.html
Fill content to the block you defined
14. <!-- home.html -->
{% block title %}Home{% endblock title %}
<!-- post.html -->
{% block title %}{{ post.title }}{% endblock title %}
<title>
{% block title_tag %}
{% block title %} Welcome {% endblock title %} | A Django Girl's Adventure
{% endblock title_tag %}
</title>
<!-- post.html -->
{% block title_tag %}{{ post.title }}{% endblock title_tag %}
Home | A Django Girl's Adventure
| A Django Girl's Adventure
15. block.super
| Welcome | A Django Girl's Adventure
<!-- post.html -->
{% block title %}{{ post.title }} | {{ block.super }}{% endblock title %}
<title>
{% block title_tag %}
{% block title %} Welcome {% endblock title %} | A Django Girl's Adventure
{% endblock title_tag %}
</title>
Use {{ block.super }} to include parent’s content
16. Common Conventions
• Prefer underscore over dash in template name, block name
• Included name of the block in endblock tag
• Template called by other template are prefixed with `_`
{% block content %} {% endblock content %}
{% include ‘_header.html’ %}
{% block title_tag %}
21. Comment in Django Template
{% comment %}
<div class="sidebar">
sidebar content is not ready
</div>
{% endcomment %}
Multi-line Comment
{# single line comment #}
Single Line Comment
<!-- HTML Comment -->