簡體   English   中英

將 url 迭代地傳遞給 django html 模板 url 標簽

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM