< Prev
Next >

Django Template - ifnotequal tag

In this tutorial, we are going to discuss the ifnotequal built-in tag in Django Template System. The functioning of ifnotequal tag corresponds to the != relational operator which is used to check the inequality between the two values.

Working of ifnotequal tag

The ifnotequal tag starts with the {% ifnotequal %} tag and ends with a {% endifnotequal %} tag. All the texts between the start and end tag is known as a block. Let us see a code with syntax of an ifnotequal tag.

{% ifnotequal var1 var2 %}
	<p>If value referenced by var1 is not equal to value referenced by var2, this will be printed</p>
{% endif %}

Note: Within the {% ifnotequal %} block, you could specifically hard-code the values to be compared for equality. These values could only be a string(defined in single/double quotes), integers, decimal numbers, or template variables and there must only be two arguments to compare.

Django project with ifnotequal tag

Up next, we are going to explain a situation is which the Django Template System encounters the ifnotequal tag in an executing HTML template file. For those who don't know how to create a Django Project from a scratch and have not read our previous tutorials, here is a recap.

Creating a view function for the app

Once we have created the HTML template file in the application, we will also need to create a view function associated with it. A view function is simply a Python function which takes an HttpRequest from the HTML template file and returns an HttpResponse with the values of the variables specified and requested by the HTML template file.

Note: This view function is defined in the file, which is already created and stored in our application directory, compare. Let us just copy and paste the following code in

We have named this view function - tag_lookup().
from django.shortcuts import render
import datetime

def tag_lookup(request):
    #Creating a dictionary of key-value pairs
    dic = { 'var1' : 100,
    'var2' : 100.0,
    'var3' : 100.50,
    'str1' : 'Hello',
    'str2' : 'hello',
    'str3' : "Hello",
    'list1': [1,,7,16), 'Make your life productive!'],
    'list2': [2,,7,16), 'Do not give up!']}
    #Calling the render() method to render the request from template_ex.htm page by using the dictionary, dic
    return render(request, "template_ex.htm", dic)

Understanding the code in :

Mapping URL to view

After creating the view function of an application, we need to specify the mapping between a URL and this view function in a file named - The file is simply coded in Python and we will have to create the file in the folder of our application, compare.

The use of mapping a URL to the view function is, when this URL is requested by the user, its mapped view function is executed and a response is returned.
from django.urls import path
from . import views

urlpatterns = [
        path('', views.tag_lookup),

As you can see in the file, we have imported the path() function from the module django.urls. The path() function has been passed the two must arguments -

Pointing the project url to application url

Next, we are going to point the project url( to the application url( by using the include() function. For this, we will open the file in the root - project directory, django_proj1, and copy-paste the following code.
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('tags', include('compare.urls')),

As you can see in the file, we have imported the path() and include() function from the module django.urls.
The path() function has been passed the two must arguments -

How to load the HTML template file.

Before we finally execute the project, we must load the above created HTML template template.htm, which is stored within our application folder compare.

To load the HTML template file, we need to specify its full path in the configuration file -, which is stored in the project folder. For our example, you can find this file in the sub-directory django_proj1 within our project django_proj1 i.e. - django_proj1/django_proj1/

Within the file, you need to locate the property DIRS and copy-paste the full-path to the HTML template file against it, as shown below and save the file.
'DIRS': ['E:/django_proj1/compare/'],

Executing the application of our project

To execute the compare application of our project django_proj1, we just have to enter the address -

As you can see in the picture, the string tags/ in the URL is the URL-pattern which gets matched to the URL pattern specified in the project file, which calls the application's and its associated view function - tag_lookup(in the application) is executed and its response is displayed.

The response displays the result of using the ifnotequal template tag in HTML Template file to compare the values of variables defined in the view function, as well as some hard-coded values.

Please share this article -

< Prev
Next >
< Template with ifequal tag
Template with elif tag >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition