8

Good Afternoon,

How can I use a variable variable name in Django templates?

I have a custom auth system using context, has_perm checks to see if the user has access to the specified section.

deptauth is a variable with a restriction group name i.e SectionAdmin. I think has.perm is actually checking for 'deptauth' instead of the variable value SectionAdmin as I would like.

{%if has_perm.deptauth %}

How can I do that? has_perm.{{depauth}} or something along those lines?

EDIT - Updated code

{% with arg_value="authval" %}
{% lookup has_perm "admintest" %}
{% endwith %}
{%if has_perm.authval %}
window.location = './portal/tickets/admin/add/{{dept}}/'+val; 
{% else %}
window.location = './portal/tickets/add/{{dept}}/'+val; 
{%endif%} 

has_perm isn't an object.. it's in my context processor (permchecker):

class permchecker(object):
def __init__(self, request):
 self.request = request
 pass
def __getitem__(self, perm_name):
 return check_perm(self.request, perm_name) 
asked Jun 17, 2013 at 13:23
2
  • has_perm is this your custom template tag? Commented Jun 17, 2013 at 13:32
  • has_perm cannot be a custom template tag. The template tag is if. Commented Jun 17, 2013 at 13:36

2 Answers 2

4

You're best off writing your own custom template tag for that. It's not difficult to do, and normal for this kind of situation.

I have not tested this, but something along these lines should work. Remember to handle errors properly!

def lookup(object, property):
 return getattr(object, property)()
register.simple_tag(lookup)

If you're trying to get a property rather than execute a method, remove those ().

and use it:

{% lookup has_perm "depauth" %}

Note that has_perm is a variable, and "depauth" is a string value. this will pass the string for lookup, i.e. get has_perm.depauth.

You can call it with a variable:

{% with arg_value="depauth_other_value" %}
 {% lookup has_perm arg_value %}
{% endwith %}

which means that the value of the variable will be used to look it up, i.e. has_perm.depauth_other_value'.

answered Jun 17, 2013 at 13:26

5 Comments

Agreed. Unfortunately, Django templates are not great for the kind of finer distinction that you're trying to make here.
Hi, I'm getting this error: 'proxy' object has no attribute 'admintest'. Looks like it's treating admintest as a property instead of a lookup string
What exactly is admintest? Is it a dictionary? Show the code you are using to call the template tag.
Hi, admintest is just a string. has_perm takes this string and checks the DB to see if the user has access to the group this string represents
{% with arg_value="authval" %} {% lookup has_perm "admintest" %} {% endwith %} {%if has_perm.authval %} window.location = './portal/tickets/admin/add/{{dept}}/'+val; {% else %} window.location = './portal/tickets/add/{{dept}}/'+val; {%endif%}
1

You can try like this,

{{ dict|key:key_name }}

Filter:

def key(d, key_name):
 return d[key_name]
key = register.filter('key', key)

More information, django ticket

answered Jun 17, 2013 at 13:46

1 Comment

This only works for a dictionary, not attributes of an object.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.