Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

firstof – Django Template Tags

  • Difficulty Level : Easy
  • Last Updated : 05 Feb, 2020

A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow paassing data from view to template, but also provides some limited features of a programming such as variables, for loops, comments, firstof, etc.
This article revolves about how to use firstof tag in Templates. firstof tag Outputs the first argument variable that is not “false” (i.e. exists, is not empty, is not a false boolean value, and is not a zero numeric value). Outputs nothing if all the passed variables are “false”.


{% firstof var1 var2 var3... %}

{% firstof var1 var2 var3 %}

This is equivalent to:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

{% if var1 %}
    {{ var1 }}
{% elif var2 %}
    {{ var2 }}
{% elif var3 %}
    {{ var3 }}
{% endif %}

One can also use a literal string as a fallback value in case all passed variables are False:

{% firstof var1 var2 var3 "fallback value" %}

firstof – Django template Tags Explanation

Illustration of How to use firstof tag in Django templates using an Example. Consider a project named geeksforgeeks having an app named geeks.

Refer to the following articles to check how to create a project and an app in Django.

Now create a view through which we will access the template,
In geeks/,

# import Http Response from django
# import Http Response from django
from django.shortcuts import render
# create a function
def geeks_view(request):
    # create a dictionary
    context = {
    # return response
    return render(request, "geeks.html", context)

Create a url path to map to this view. In geeks/,

from django.urls import path
# importing views from
from .views import geeks_view
urlpatterns = [
    path('', geeks_view),

Create a template in templates/geeks.html.

<h3>Variable displayed : </h3>
{% firstof var1 var2 var3 %}

Let’s check if data is displayed from the third variable in geeks.html

Advanced Usage

This tag auto-escapes variable values. You can disable auto-escaping with:

{% autoescape off %}
    {% firstof var1 var2 var3 "fallback value" %}
{% endautoescape %}

Or if only some variables should be escaped, you can use:

{% firstof var1 var2|safe var3 "fallback value"|safe %}

You can use the syntax {% firstof var1 var2 var3 as value %} to store the output inside a variable.

My Personal Notes arrow_drop_up
Recommended Articles
Page :