Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use the cycle template tags in Django

Njoku Ifeanyi Gerald

A Django template is a text document, like HTML, with Jinja syntax.

The syntax of the Django template language involves four constructs which are {{ }} or {% %}.

In this shot, we will look into cycles as a Django template tag. The cycle tag is useful in loops and is used to cycle through a list of arguments. It produces an argument of its own during a looping process.


An example of how to use the cycle template is provided below.

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


In the above example, the first element in queryset is assigned class row1. The second item is assigned row2. The third is also assigned row1 since the cycling arguments end and are so repeated until all the elements of queryset have been assigned either of the classes.

Making a Django application

The steps below show how to make a Django application that uses the cycle template.

Step 1

Add these commands on the command line.

pip is the package installer for Python.

pip install pipenv
pipenv shell
pipenv install django

Step 2

django-admin startproject DjangoCircle ./
python startapp 

Step 3

python migrate
python runserver

Step 4

Go to and do the following. helps register your app and packages that are being installed write rules.

# Application definition




Step 5

In the codebase app folder, go to

This file is responsible for creating the model for the database

from django.db import models
from django.urls import reverse

class CRUD(models.Model):
    name = models.CharField(max_length=30)
    age = models.IntegerField()
    level = models.CharField(max_length=30)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):

    def update_url(self):
        return reverse('update', kwargs={"id":})

Step 6

Go to In this file, we register our previously created model by importing it. By doing so, we can see the model when logged in as admin.

from django.contrib import admin
from .models import CRUD

Step 7

The file is used to create functions or classes that visualize how a route will operate.

from django.shortcuts import render
from .models import CRUD

def home(request):
    queryset = CRUD.objects.all().order_by('-date')
    context = {
        'queryset': queryset
    return render(request, 'app/base.html', context)

In the codebase app, create a folder and name it “templates.”

Inside the “templates” folder, create another folder and name it “app.” Then, inside the “app” folder, create the base.html file.

Here, we see how to use circles to display our data in HTML.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- bootstrap -->
    <link href="" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <div class="container mt-3">
        <!-- the block is used to loop the dataset -->
        {% for data in queryset %}
        <!-- setting using cycle in the template -->
        <div class="{% cycle 'row1' 'row2' %}">
            <!-- double curly braces use to display data -->
        <!-- end of the forloop -->
        {% endfor %}   

Step 8

In the DjangoCircle folder, in the file do the following.

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

urlpatterns = [
    # including the codebase file
    path('', include('codebase.urls'))

Step 9

Then in the “codebase” folder, create a file and name it

from django.urls import path
from .view import home

urlpatterns = [
    path('',home, name="home" ),

Step 10

Run the following commands.

python makemigrations

python migrate

Step 11

Create a superuser and input data in it:

python createsuperuser

Step 12

After that, run:

python runserver

Then go to to access the admin page and create data there or to access the homepage.





Njoku Ifeanyi Gerald

View all Courses

Keep Exploring