For the loop variable used in templatetag?

Trying to solve a problem with templatetags.

I have two templatetags:

@register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names): """ Return a list of letters that last names start with. """ return { 'letters': [name.last_name[0] for name in names] } class NameNode(template.Node): def __init__(self, letter, var_name): self.letter = letter self.var_name = var_name def render(self, context): context[self.var_name] = Person.objects.get_active().filter( last_name__istartswith=self.letter) return '' @register.tag def get_names(parser, token): try: tag_name, arg = token.contents.split(None, 1) except ValueError: raise template.TemplateSyntaxError, "%r tag requires arguments" % \ token.contents.split()[0] m = re.search(r'for (.*?) as (\w+)', arg) if not m: raise template.TemplateSyntaxError, "%r tag had invalid arguments" % \ tag_name letter, var_name = m.groups() return NameNode(letter, var_name) 

And I want to use them like this:

 {% for letter in letters %} <h2>{{ letter }}</h2> {% get_names for letter as names %} <ul> {% for name in names %} <li>{{ name }}</li> {% endfor %} </ul> {% endfor %} 

But a "letter" is sent as the word "letter", and not like any letter that a variable should contain. Is there a way around this or something that I am missing (or, even better, a package that already does this)?

+4
source share
1 answer

You get the string "letter" because this is what the template parser reads from your template source. If you want to access the value of this variable, you must mark it as a template variable in NameNode.__init__ and enable it using context in render .

Properly:

 class NameNode(template.Node): def __init__(self, letter, var_name): self.letter = template.Variable(letter) self.var_name = var_name def render(self, context): try: context[self.var_name] = Person.objects.get_active().filter( last_name__istartswith=self.letter.resolve(context)) except template.VariableDoesNotExist: pass return '' 

This is explained in the docs here .

+4
source

Source: https://habr.com/ru/post/1306734/


All Articles