The simple_tag
helper does not allow you to assign the result to a context variable in this way. Try using assignment_tag
instead.
Loading template tag variables for use in template
-
29-11-2021 - |
문제
How do I load the results of a templatetag into a a template to iterate over? Basically I am aiming to load the tags on a model object (using django-tagging) and then iterate through the tags to create a list of related products based on those tags. Then I would like to iterate through those product objects to display more information about them.
Ex, my template tag:
@register.simple_tag
def get_rel_from_tag(tag_list):
try:
relproducts = UniPart.objects.filter(part__contains = partbase)
except:
print "no related products"
return None
else:
relproducts = UniPart.objects.filter(part__contains = partbase)
return relproducts
How do I make it so that relproducts
is returned as a variable? This is how I call it in the template:
{% tags_for_object design as tag_list %}
{% get_rel_from_tag tag_list %}
Basically now I want to iterate over relatedprod now but it's not working.
해결책
다른 팁
Did you load the template tag file using {% load 'your_file_name %}
Update:Try using 'with' to cache the result from tags_for_object_design
{% with tag_list=tags_for_object design %}