Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6043203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:52:25+00:00 2026-05-23T06:52:25+00:00

I’m trying to get a custom template tag to work but am having some

  • 0

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.

I’d like to have the tag look as below and only display the text “WORKED” if my permissions function evaluates to True.

Template code

{% load permission_tags %}
{% for group in groups %}
<div>{% permission request.user can_edit_group on group %}WORKED{% endpermission %}</div>
{% endfor %}

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.

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

"Caught AttributeError while rendering: 'User' object has no attribute 'resolve'" 
templatetags/permission_tags.py in render, line 23  (I've marked line 23 below)

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.

Template tag called permissions in templatetags/permission_tags.py

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)   # <---- 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)

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?

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.

Thank you,
Joe

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:52:26+00:00Added an answer on May 23, 2026 at 6:52 am

    When you change back self.user to a User instance here:

    self.user = self.user.resolve(context)
    

    it works the first time, but the next time, because self.user is no longer a template.Variable instance, you get the exception: ‘User’ object has no attribute ‘resolve'”

    One solution is to save user & object instance in local variables:

    def render(self, context):
    
        user_inst = self.user.resolve(context)
        object_inst = self.object.resolve(context)
    
        permissions_obj = object_inst.permissions(user_inst)
    
        content = self.nodelist.render(context)
    
        if hasattr(permissions_obj, self.permission):
            perm_func = getattr(permissions_obj, self.permission)
            if perm_func():
                return content 
        return ""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.