I wrote a custom tag to loop through a list of tuples and give a seperate variable for each key and value. I want it to do the following. list=[('one',1),('two',2)] for a,b in list: return a,b #return these to context variables. {{a}} = {{b}}
Right now the custom tag is very bare, it doesn't have any error handling, I just want to get the basic logic to work. The problem I am having is that the loop only returns the last item. I feel like I am missing something small but I don't know what. from django.core import template register = template.Library() # use {% loop_list a in alist as b %} def do_loop_list(parser,token): bits = token.contents.split() loopkey = bits[1] #a loopval = bits[5] #b sequence = parser.compile_filter(bits[3]) nodelist = parser.parse(('endlooplist',)) parser.delete_first_token() return LoopListNode(loopkey, loopval,sequence,nodelist) do_loop_list = register.tag("loop_list", do_loop_list) class LoopListNode(template.Node): def __init__(self, loopkey, loopval, sequence, nodelist): self.loopkey, self.loopval, self.sequence = loopkey, loopval, sequence self.nodelist = nodelist def render(self, context): try: values = self.sequence.resolve(context) except VariableDoesNotExist: values = [] context['ur_values'] = values #debug to see what the list looks like for a,b in values: context[self.loopkey] = a context[self.loopval] = b return self.nodelist.render(context) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---