1

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.

user1328021
  • 9,110
  • 14
  • 47
  • 78

2 Answers2

1

The simple_tag helper does not allow you to assign the result to a context variable in this way. Try using assignment_tag instead.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
0

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 %}
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
  • Yes it's in the same file as the `tags_for_object` template tag which is working fine and returned `tag_list` properly. – user1328021 Nov 14 '12 at 16:37