[Answer]-Django โ€“ automating certain template behaviours

1๐Ÿ‘

โœ…

You are going to love the extends and block tags.

Assuming you already got a templated page working, you can extract the basic setup of your HTML page like this:

Create a template called base.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>{% block title %}{% endblock %}</title>
    {% block head %}{% endblock %}
  </head>
  <body>
    {% block body %}
      Empty page.
    {% endblock %}
  </body>
</html>

Now, in your page template called page.html, you can extend your base template and override any blocks:

{% extends "base.html" %}
{% block title %}Page 1 title{% endblock %}
{% block body %}
   Real page content.
   {% block main %}
      Subpage of page.html can also override this main block.
   {% endblock %}
{% endblock %}

But Hamish is right, do checkout the doc:
https://docs.djangoproject.com/en/dev/topics/templates/

๐Ÿ‘คZiyan

Leave a comment