I created dynamic slider using bootstrap. I want to show number indicator for slider, so I added one to variable in forloop. But I m getting the following error "TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'".
Custom Filter Example:
This is just an example for custom filter.
Resolve with custom filter:
In django, we cannot perform arithmetic operations using template tags. To achieve this I created a custom template filter.
Create a folder named "templatetags" in django app and create a empty file named "__init__.py" inside the folder. Then,
Create a template filter in django app, "Custom_filters.py".
Copy and pasted below code:
# custom_filters.py from django import template register = template.Library() @register.filter def add_one(value): return value + 1
In Html Template, load the custom filter at the top of the template.
{% load custom_filters %}
Use the custom filter to add 1 to the 'forloop.counter'
{% for item in sliders %} <li data-bs-target="#myCarousel" data-bs-slide-to="{{ forloop.counter0|add_one }}" {% if forloop.first %} class="active" aria-current="true" {% endif %} aria-label="Slide {{ forloop.counter0|add_one }}">{{ forloop.counter0|add_one }}</li> {% endfor %}
OUTPUT:

Video Guide
Post your comments / questions
Recent Article
- How to get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0
- CommandError- Conflicting migrations detected multiple leaf nodes in the migration graph
Related Article