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.

rstuart85
  • 2,035
  • 2
  • 15
  • 19
  • If this is indeed django you could create a custom template tag to return the private var. – Colleen Dec 03 '12 at 23:56
  • a google search of "django template underscore vars" reveals: https://groups.google.com/forum/?fromgroups=#!topic/django-users/J8c0j2_iEcc -- 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 03 '12 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 04 '12 at 00:12

2 Answers2

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" }}
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • Thanks for the solution! I started work on a package to collect useful underscore filters. I'm mentioning you. https://github.com/halfnibble/django-underscore-filters – Nostalg.io Dec 31 '14 at 02:28
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.

Checo R
  • 862
  • 14
  • 22