what is {% block content %} and {% endblock content %} for in Django?

后端 未结 2 1672
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 01:49

so I just started reading a book on Django (for beginners) and I came across the following code snipet:

Home
2条回答
  •  抹茶落季
    2021-02-07 02:23

    block is used for overriding specific parts of a template.

    In your case, you have a block named content and this is supposed to be overridden by children that inherit from this template.

    From the examples at The Django Docs

    Template to be extended, named base.html

    
        
        {% block title %}My amazing site{% endblock %}
    
    

    Overriding Child template

    {% extends "base.html" %}
    
    {% block title %}My amazing blog{% endblock %}
    

    "My amazing site" will be overriden by the child and then display "My amazing blog"

提交回复
热议问题