My Custom tag:
# app/templatetags/ctags.py
def getgenre():
genre = ["Test1", "Test2"]
return genre
register.simple_tag(getgenre)
My html:
# app/templates/base.html
{% load ctags %}
<!-- {% getgenre %} -->
{% for genre in getgenre %}
<li>{{genre}}</li>
{% endfor %}
This renders a blank page for me. If I uncomment {% getgenre %}, django renders ["Test1", "Test2"] as expected. I've tried countless variations of setting up my tag (including the non-simple_tag way) to no avail. I am simply unable to iterate over any value returned by one of my custom-tags.
Am I missing something fundamental here?
You shoud use assignment_tag
instead of simple_tag
:
@register.assignment_tag
def getgenre():
genre = ["Test1", "Test2"]
return genre
And then in the template:
{% load ctags %}
{% getgenre as genre_list %}
{% for genre in genre_list %}
<li>{{genre}}</li>
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With