Django Templates
5 min read
Use Jinja2-style Django templates: extends, block, for, if, and built-in filters.
Django Templates
{# templates/base.html #}
<!DOCTYPE html>
<html>
<body>
{% block content %}{% endblock %}
{% block scripts %}{% endblock %}
</body>
</html>
{# templates/blog/post_list.html #}
{% extends "base.html" %}
{% load humanize %}
{% block content %}
{% for post in posts %}
<h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<p>By {{ post.author }} | {{ post.created_at | timesince }} ago</p>
<p>{{ post.body | truncatewords:30 }}</p>
{% empty %}
<p>No posts yet.</p>
{% endfor %}
{# Pagination #}
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">Next</a>
{% endif %}
{% endblock %}