TAGS :Viewed: 6 - Published at: a few seconds ago

[ django template tag instance ]

hard to word the question so ill go right to the point, i wrote the following template tag

def do_simple_tag(parser, token):
    try:
        tag_name, name = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires exactly one argument" % token.contents.split()[0])
    if not (name[0] == name[-1] and name[0] in ('"', "'")):
        raise template.TemplateSyntaxError("%r tag's argument should be in quotes" % tag_name)
    return SimpleTagNode(name[1:-1])


class SimpleTagNode(template.Node):
    def __init__(self, name):
        self.name = name
    def render(self, context):
        content = get_content(context, request, name)
        return content

register.tag('simple_tag', do_simple_tag)

then i wrote a function that scans for this tag in a template and gets all instances of this tag within said template in a list like so

def get_tags(template):
    compiled_template = get_template(template)
    simple_tag_instances = _scan_tag(compiled_template.nodelist)

def _scan_tag(nodelist, current_block=None, ignore_blocks=[]):
    tags = []
    for node in nodelist:
        if isinstance(node, SimpleTagNode):
            tags.append(node.get_name())

so, my question is why does the isinstance fail if node is infact an instance of SimpleTagNode ( or so i believe ) , i checked nodelist and saw that indeed there were instances of SimpleTagNode, but they would all return false in the isinstance condition, i have spent a long time trying to figure this one out, but found nothing, i even used the shell running the funcions above and still returned fals, any help is much a appreciated

Answer 1


So i finally solved it, basically in the module that contained the _scan_tag function at the top of the file i was importing the SimpleTagNode class like so

from simple_tag.templatetags.simple_tag import SimpleTagNode

simple_tag being the name of my app, and also the name of the template file, for some reason this was conflicted with isinstance , so i tried

from paulo.simple_tag.templatetags.simple_tag import SimpleTagNode

paulo being my project app, and it worked.