[英]Pass urls iteratively into django html template url tag
我有一個主頁模板,其中包含指向我想迭代呈現的許多其他頁面的鏈接:(由於 python 語法,這顯然不起作用)
{% for idx, category in enumerate(categories) %}
<div class="box">
a href={% url 'category_links[idx]' %}> {{category}}</a>
div>
{% endfor %}
我有以下網址,我也從我的角度傳遞給我的模板:
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']
我知道{{forloop.counter0}}
但無法正確集成它。
我將不勝感激!
您可以通過簡單地制作自定義模板過濾器來使用 {{ forloop.counter }} 循環低谷列表
IE :與在 python 中使用list_object[index]
相同
templatetags/index.py (在app根目錄下創建一個templatetags文件夾)
from django import template
register = template.Library()
@register.filter
def index(list_object, i):
return list_object[i]
模板.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 %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.