Check if Home Page in Django

In this tutorial, we will go through two different ways to check if the current page is the home page in Django.


Use request to Check if Home Page

The easiest way to see if the current page is the root page in a Django template is to use the request.path property. If the value of request.path is equal to '/' it is the home page.


Here is an example where any code inside the if block will run on the home page only.


{% if request.path != "/" %}
  <!-- Code to run -->
{% endif %}


Check Current Page in Django with current_page

Another way to see if the home page is the current page in Django is to use the current_page.is_home property.


{{ current_page.is_home }}