How to add Site Header, Site Title, Index Title in a Django Project?


To make it simple for people to browse and comprehend the goal of the site, it's critical to have a clear and succinct site header, site title, and index title when constructing a Django project. You must specify the site header, site title, and index title in your Django application's HTML templates in order to add them to the site. Every page of your website will have these components, making it simpler for visitors to browse and comprehend the goal of your project. These additions are especially helpful for complicated, huge websites that users may have trouble navigating. We will go over adding these components to your Django project using Python code in this tutorial.

Algorithm

To add index, site header, and page title to your Django project using Jinja 2 blocks, follow these steps −

  • Create a base HTML template for your project. This template should contain the necessary Jinja 2 blocks for index, site header, and page title.

  • In the base template, define the Jinja 2 blocks for index, site header, and page title using the {% block %} syntax.

  • In each view of your Django application, extend the base template and provide content for the respective Jinja 2 blocks using the {% extends %} and {% block %} syntax.

  • Render the extended template in your Django application using the {% include %} or {% extends %} syntax.

base.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>{% block title %}{% endblock %}</title>
</head>
<body>
   <header>
      <h1>{% block header %}{% endblock %}</h1>
   </header>
   <main>
   {% block content %}{% endblock %}
   </main>
</body>
</html>

index.html

# In your index template (index.html)

{% extends "base.html" %}

{% block title %}Index Title{% endblock %}

{% block header %}Site Header{% endblock %}

{% block content %}
   <h2>Index Title</h2>
   <p>Make up some text to place here : : : </p>
{% endblock %}
  • In Django, block header and block titles are part of the template system that allows you to create reusable templates with dynamic content. All this falls under the Jinja2 templating which is interpolated in HTML itself

  • {% block title %}{% endblock %} allows defining a block for the title of a web page in the head section of an HTML file but note that it is empty by default so you are expected to override it in the child template, allowing you to dynamically set the title for each page of your site. For example, if you have a blog with multiple articles, you can set the title of each article page dynamically by using the block title.

  • {% block header %}{% endblock %} helps in defining a block for the header section of a web page in the body section of an HTML file. Similar to the above block, it is also empty by default and is supposed to be overridden in the child template, allowing you to set the header content dynamically for each page of your site.

  • To use these blocks create a base template with the basic structure of your site, including the head and body sections. Then, create child templates that inherit from the base template and override the {% block title %}{% endblock %} and {% block header %}{% endblock %} blocks to set the specific content for each page.

Conclusion

In conclusion, adding index, site header, and page title blocks to your Django project using Jinja 2 may significantly improve the overall appearance and functionality of your application. You may simply maintain uniformity throughout your project while still allowing for flexibility in individual pages by establishing a base HTML template with the essential blocks and expanding it in each view with the relevant information. The syntaxes "block%," "extends%," and "include%" offer a simple and effective way to accomplish this. Overall, Jinja 2 blocks are a potent tool for using Django to build dynamic and attractive web applications.

Updated on: 21-Aug-2023

218 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements