Django templates: value of dictionary key with a space in it

后端 未结 2 682
离开以前
离开以前 2020-12-01 16:23

In a Django template, is there a way to get a value from a key that has a space in it? Eg, if I have a dict like:

{\"Restaurant Name\": Foo}
<
2条回答
  •  天涯浪人
    2020-12-01 16:37

    You can use a custom filter as well.

    from django import template
    register = template.Library()
    
    @register.filter
    def get(mapping, key):
      return mapping.get(key, '')
    

    and within the template

    {{ entry|get:"Restaurant Name" }} 
    

提交回复
热议问题