django multiple template inheritance - is this the right style?

后端 未结 1 808
感情败类
感情败类 2020-12-16 11:47

Yo

so i have a base.html:


    
        
...
{% block main %}{% endblock %}
相关标签:
1条回答
  • 2020-12-16 11:53

    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.

    0 讨论(0)
提交回复
热议问题