10

I am trying to access elements of a dict with keys that start with the underscore character. For example:

my_dict = {"_source": 'xyz'}

I'm trying to access them in a Django template. Obviously I realise that you can't access underscored python variables from a Django template (because they are considered private in Python) but this is a dict object where any immutable object is a valid key.

I can't access the above dict in a Django template using {{ my_dict._source }} so I assume Django is preventing it. Is that accurate?

I am kind of hoping Django does something sane with variables that start with underscore like still doing dict lookups (the first thing is supposedly tries) but refuses to do attribute lookups, method calls and list index lookups since an underscored prefixed variable would be invalid. I am quickly loosing hope though.

For the record, I know someone will suggest to just change the dict but this is actually a multi-levelled dictionary returned by the rawes library when executing REST API request on a ElasticSearch instance.

3
  • If this is indeed django you could create a custom template tag to return the private var.
    – Colleen
    Dec 3, 2012 at 23:56
  • a google search of "django template underscore vars" reveals: groups.google.com/forum/?fromgroups=#!topic/django-users/… -- i.e. yes, you will need to create a custom template tag. Though this thread is old I doubt that functionality has changed.
    – Colleen
    Dec 3, 2012 at 23:57
  • It isn't a private var, it is a perfectly legal dict key, something that should be accessible with a dict lookup. Really trying to avoid making a template tag. Especially since the dict objects can get quite deep like {{ my_dict.hits._source._text }}.
    – rstuart85
    Dec 4, 2012 at 0:12

2 Answers 2

18

The docs mention that you can't have a variable start with an underscore:

Variable names must consist of any letter (A-Z), any digit (0-9), an underscore (but they must not start with an underscore) or a dot.

but you can easily write a custom template filter to mimic the dictionary's get method:

@register.filter(name='get')
def get(d, k):
    return d.get(k, None)

and

{{ my_dict|get:"_my_key" }}
1
0

In my case, if I know the dict elements, and it's only one, I prefer to rename the dict key using pop:

my_dict['new_key'] = my_dict.pop('_old_key')

That way I get a new name on the dict, and I can access in the template without problems.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.