[Django]-Django multiple template inheritance – is this the right style?

29πŸ‘

βœ…

Yo. πŸ™‚

The answer depends on how much your templates have in common.

  • If your templates do have much in common, i.e. they are pages of some section of your site, or just have a very common structure, then your way of doing it would be correct. I just think that you should use more descriptive names for the blocks.

    {% extends base.html %}
    {% block page_heading %}{% endblock %}
        ... snazzy code here that shows all the posts by all the users ...
    {% block extra_content %}{% endblock %}
    
  • If your templates don’t have much in common, but share some specific block of content, then it’s a different situation since it’s difficult to make a properly inherited structure. You should use the {% include %} tag in this case. For example, make another template that shows posts, say _list_posts.html, then use it in the children templates.

    {% extends base.html %}
    {% block main %}
    <h1>Welcome to your posts hangout!</h1>
    {% include '_list_posts.html' %}
    {% endblock %}
    

    You could also use the inclusion tag for that.

So, which option should you choose? Try to answer the question: should these two templates have a common parent? If yes, go for the first option. Otherwise, go for the second option.

πŸ‘€DrTyrsa

Leave a comment