Advertisement



< Prev
Next >



Django Template - if/else tag



In the last tutorial, we have discussed the built-in if tag in the Django Template System. The if tag can be associated with an optional else tag, where the if tag corresponds to the if conditional statement which is used when we want to test a condition for a boolean value - true or false and the else tag corresponds to the else in the if-else conditional statement.

Note: Instead of a condition, there could even be a variable which returns a non-zero value or just a non-zero value.




Working of if/else tag




Let us see a code with syntax of an if tag with an optional else else tag.

{% if var %}
	<p>This is associated with the if tag </p>
{% else %}
	<p>This is associated with the else tag </p>
{% endif %}





Note:


While we could use relational and logical operators with the if tag, we cannot use relational or logical operators with the optional else tag to test a condition expression involving variables.




Django project with if/else tag


Up next, we are going to explain a situation is which the Django Template System encounters the if tag and its associated else 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 view.py, which is already created and stored in our application directory, compare. Let us just copy and paste the following code in view.py.

We have named this view function - tag_lookup().

view.py
from django.shortcuts import render
import datetime

def tag_lookup(request):
    #Creating a dictionary of key-value pairs
    dic = { 'var1' : 100,
    'var2' : 200,
    'var3' : 300 }
	
	
    #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 view.py :



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 - urls.py. The file urls.py is simply coded in Python and we will have to create the urls.py 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.

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

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

As you can see in the file urls.py, 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(urls.py) to the application url(urls.py) by using the include() function. For this, we will open the file urls.py in the root - project directory, django_proj1, and copy-paste the following code.

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

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

As you can see in the file urls.py, 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 - settings.py, 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/settings.py.


Within the settings.py 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 - http://127.0.0.1:8000/tags/.


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 urls.py file, which calls the application's urls.py 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 if template tags with else template tags in HTML Template file to compare the values of variables var1, var2 and var3 and also hard-coded values using the relational and logical operators, where the variables are defined in the view function.




Please share this article -




< Prev
Next >
< Template with if tag
Template with for tag >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement