Related Articles

Related Articles

cycle – Django Template Tags
  • Last Updated : 30 Jan, 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, cycle etc.
This article revolves about how to use cycle tag in Templates. It produces one of its arguments each time this tag is encountered. The first argument is produced on the first encounter, the second argument on the second encounter, and so forth. Once all arguments are exhausted, the tag cycles to the first argument and produces it again.

Syntax
{% cycle 'value_1' 'value_2' %}
Example

This tag is particularly useful in a loop:

filter_none

edit
close

play_arrow

link
brightness_4
code

{% for o in some_list %}
    <tr class="{% cycle 'row1' 'row2' %}">
        ...
    </tr>
{% endfor %}

chevron_right


The first iteration produces HTML that refers to class row1, the second to row2, the third to row1 again, and so on for each iteration of the loop.

Cycle – Django template Tags Explanation

Illustration of How to use cycle 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 pass the context dictionary,
In geeks/views.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

# import Http Response from django
from django.shortcuts import render
  
# create a function
def geeks_view(request):
    # create a dictionary
    context = {
        "data" : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    }
    # return response
    return render(request, "geeks.html", context)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Create a template in templates/geeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

{% for i in data %}
    <div class="{% cycle 'row1' 'row2' %}">
        {{ i }}
    </tr>
{% endfor %}

chevron_right


Let’s check what is displayed on “/” are displayed in the template.
cycle-django-template-tags
Let’s check page source for the same.
cycle-tag-django-templates

Advanced Usage

One can use variables, too. For example, if you have two template variables, rowvalue1 and rowvalue2, you can alternate between their values like this:

filter_none

edit
close

play_arrow

link
brightness_4
code

{% for o in some_list %}
    <tr class="{% cycle rowvalue1 rowvalue2 %}">
        ...
    </tr>
{% endfor %}

chevron_right


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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :