Search code examples
pythonhtmldjangojinja2template-inheritance

django template inheritance: how to NOT display a block from parent template?


how can we hide a block in child template which is being rendered by paent template?

for ex: my parent template base.html contains-

<!DOCTYPE html>
<html lang="en">
....
<body>
{% block messages %}
      <div class="alert alert-{% if message.tags == 'error'%}danger{% else %}{{ message.tags }}{% endif %} alert-dismissible fade in" role="alert">
             {{message}}
      </div>
{% endblock %}
...
</body>
</html>

and I have inherited this base.html in login.html but I do not want to use {% block messages %} in login.html, any suggestions? Thanks in advance for any solution😊.


Solution

  • You may override {% block messages %} in your login.html, like that:

    login.html

    {% extends "base.html" %}
    {% block messages %}{% endblock %}
    ...