Django-无法遍历自定义模板标签

问题描述 投票:0回答:1

我已经如下创建了自定义categories_tag模板标签:

from django import template
register = template.Library()

from ..models import Category

@register.simple_tag(takes_context=True)
def categories_tag(context):
    categories = Category.objects.all()

    return categories

现在想像这样循环遍历此标签:

<h3>CATEGORIES</h3>
{% for i in categories_tag %}
   <a href="">i</a>
{% endfor %}

但是在浏览器中似乎什么都没有。我的代码有什么问题?

python django django-templates templatetags
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.