Debug School

rakesh kumar
rakesh kumar

Posted on

How to change the header tag in your base.html template based on the route URL in Django

base.html:

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
    <header>
        <h1>{% block header %}Default Header{% endblock %}</h1>
    </header>

    <div class="content">
        {% block content %}
        {% endblock %}
    </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In your specific templates that inherit from base.html, you can override the header block and set the appropriate header tag content based on the route URL. Here's an example for two different routes:

home.html:

{% extends 'base.html' %}

{% block header %}
    <h1>Welcome to My Website</h1>
{% endblock %}

{% block content %}
    <!-- Content for the home page -->
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

about.html:

{% extends 'base.html' %}

{% block header %}
    <h1>About Us</h1>
{% endblock %}

{% block content %}
    <!-- Content for the about page -->
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)