Accessing dict elements with leading underscores in Django Templates

前端 未结 2 2132
北荒
北荒 2020-12-10 15:52

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 16:15

    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.

提交回复
热议问题