I have a home template with links to many other pages which I want to render iteratively: (this obviously does not work due to python syntax)
{% for idx, category in enumerate(categories) %}
<div class="box">
a href={% url 'category_links[idx]' %}> {{category}}</a>
div>
{% endfor %}
I have the following urls which I also pass to my template from my view:
category_links = ['journals', 'proceedings', 'books', 'articles_in_books', 'talks', 'poster', 'habilitations', 'phd_thesis', 'bachelor_master_diploma_thesis', 'lectures', 'conferences', 'calls_and_awards', 'telegrams_and_circulars']
I am aware of {{forloop.counter0}}
but was unable to integrate it properly.
I would greatly appreciate some help!
You can loop trough list with {{ forloop.counter }} by simply make a custom template filter
IE : same as use list_object[index]
in python
templatetags/index.py (create a templatetags folder in the app root directory)
from django import template
register = template.Library()
@register.filter
def index(list_object, i):
return list_object[i]
template.html
{% load index %}
{% for category in categories %}
{% with forloop.counter0 as i %}
<div class="box">
a href='{% url category_links|index:i %}'> {{category}}</a>
<div>
{% endwith %}
{% endfor %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.