title: Django: custom template tag errors in for loop
body: <p>I'm trying to get a custom template tag to work but am having some difficulties when I apply the tag within a for loop with multiple items in the thing being iterated across. </p>
  
  <p>I'd like to have the tag look as below and only display the text "WORKED" if my permissions function evaluates to True.  </p>
  
  <p><strong>Template code</strong></p>
  
  <pre><code>{% load permission_tags %}
  {% for group in groups %}
  &lt;div&gt;{% permission request.user can_edit_group on group %}WORKED{% endpermission %}&lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p>The tag basically takes in a user instance, a permissions string (i.e. "can_edit_group"), an "on" keyword (just to make the syntax nice) and an object instance to check permissions on.  </p>
  
  <p>The permissions framework I have going here I think is working ok and is not really part of my question.  The difficulty I am having is </p>
  
  <pre><code>"Caught AttributeError while rendering: 'User' object has no attribute 'resolve'" 
  templatetags/permission_tags.py in render, line 23  (I've marked line 23 below)
  </code></pre>
  
  <p>template error when the for loop contains more than one group.  The tag works nicely with just one group, but bombs out if I add more than one. </p>
  
  <p><strong>Template tag called permissions in templatetags/permission_tags.py</strong></p>
  
  <pre><code>from django import template
  register = template.Library()
  
  def permission(parser, token):
      try:
          tag_name, username, permission, onkeyword, object = token.split_contents()
      except ValueError:
          raise template.TemplateSyntaxError("%r tag requires exactly 4 " 
                   "arguments" % token.contents.split()[0])
      nodelist = parser.parse(('endpermission',))
      parser.delete_first_token()
      return PermissionNode(nodelist, username, permission, object)
  
  
  class PermissionNode(template.Node):
      def __init__(self, nodelist, user, permission, object):
          self.nodelist = nodelist
          self.user = template.Variable(user)
          self.permission = permission
          self.object = template.Variable(object)
  
      def render(self, context):
  
          self.user = self.user.resolve(context)   # &lt;---- Line 23
          self.object = self.object.resolve(context)
  
          # My custom permissions code.  I don't think it's
          # causing the error I am experiencing
          permissions_obj = self.object.permissions(self.user)
  
          content = self.nodelist.render(context)
  
          # My custom permissions code.  I don't think it's causing
          # the error.
          if hasattr(permissions_obj, self.permission):
              perm_func = getattr(permissions_obj, self.permission)
              if perm_func():
                  return content 
          return ""
  
  register.tag('permission', permission)
  </code></pre>
  
  <p>Do you have any idea why this template tag generates an error when I have more than one item in my for loop but succeeds when I have just one?   </p>
  
  <p>I don't quite yet understand all of the inner-workings of this template tags syntax, so I have a feeling I have made a logic error somewhere.  Any advice is much appreciated. </p>
  
  <p>Thank you,
  Joe</p>