How to Use If Statement in Django Template ?

Published On: 04/08/2022 | Category: Django


Hi Dev,

In this article we will cover on how to implement if statement in django python template example. I would like to share with you how to use if/else condition on python django templates?. let’s discuss about using if else condition in python django template. I explained simply about nested if else in django template. Let's get started with if else tag in django template example.

So, in this example we can check the two numbers is greater than first number and in second example check the odd and even number.

Here i explained simply step by step example of how to generate qr code in django python.

Example : 1

In this step, we need to create the views for performing the get number.Open the core/views.py file and add:

core/views.py
from django.shortcuts import render

def index(request):
    number1 = int(request.GET.get('number1', 0))
    number2 = int(request.GET.get('number2', 0))
    dict={
        'number1' : number1,
        'number2' : number2
    }
    return render(request, 'index.html', dict)

Next, open the core/templates/index.html file and the add:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>If Statement in Django</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h3>How to Use If Statement in Django Template ?</h3>
                    </div>
                    <div class="card-body">
                        <form action="" method="get">
                            <div class="form-group">
                                <label> First number: </label>
                                <input type="text" name="number1" class="form-control" placeholder="Enter First number"><br>
                            </div>
                            <div class="form-group">
                                <label> Second number: </label>
                                <input type="text" name="number2" class="form-control" placeholder="Enter Second number">
                            </div>
                            <button type="submit" class="btn btn-success">Submit</button>
                        </form>
                        <div class="row">
                            <div class="col-md-12 mt-3">
                                {% if number1 > number2 %}
                                    <h3>{{number1}} is greater than {{number2}}</h3>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Example : 2

In this second example, we will to create the views for performing check odd and even number from input through get method.Open the core/views.py file and add:

core/views.py
from django.shortcuts import render

def oddEven(request):
    number = int(request.GET.get('number', 0))
    return render(request, 'oddeven.html',{'number',number})

Next, open the core/templates/oddeven.html file and the add:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>If Statement in Django</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5 pt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h3>How to Use If Statement in Django Template ?</h3>
                    </div>
                    <div class="card-body">
                        <form action="" method="get">
                            <div class="form-group">
                                <label> Number: </label>
                                <input type="text" name="number" class="form-control" placeholder="Enter number"><br>
                            </div>
                            <button type="submit" class="btn btn-success">Submit</button>
                        </form>
                        <div class="row">
                            <div class="col-md-12 mt-3">
                                {% if number|divisibleby:"2" %}
                                  <h4>The number is even</h4>
                                {% else %}
                                  <h4>The number is odd</h4>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Step 7 : Creating URLs

In this section, we’ll create the urls to access our core views.Go to the urls.py core/urls.py file and update it as follows:

core/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),
    path('odd-even/', views.oddEven),
]

Next, we will require the modify the urls.py your root preoject folder lets update the file.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

Next, go to the http://localhost:8000/ address with a web browser.

I Hope It will help you....