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

795 views Asked by At

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.

2

There are 2 answers

3
Nikita Zhuikov On BEST ANSWER

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

login.html

{% extends "base.html" %}
{% block messages %}{% endblock %}
...
1
Mohamed ElKalioby On

As a side note, it is a better practice to have a base for login and registration which is different from your application base especially if you need users to login as alot of stuff won't show on these pages like the navbar.