具有Django模板空间的dict键 [英] dict keys with spaces in Django templates
问题描述
我试图从我的view.py在HTML模板中提供一个字典,例如:
test = {'作品':True,'this failed':False}
并在模板中:
这样做没有问题:
{{test.works}}
但是,在这个失败之间的字之间有一个空格的字典键不起作用:
{{test.this failed}}
我收到这个错误:
无法解析剩余部分:
'$'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ >如何克服这个问题?我不是填写模型的,所以我不能改变dict的键来删除空格。
解决方案过滤器你想要的是像
@ register.filter(name ='getkey')
def getkey(value, arg):
返回值[arg]
与
$ b一起使用
$ b{{test | getkey:'this works'}}
来源: http://www.bhphp.com/blog4.php/2009/08/17/django-templates-and-dictionaries
I am trying to present a dictionary from my view.py at the HTML template such as:
test = { 'works': True, 'this fails':False }
and in the template:
This works without a problem:
{{ test.works }}
But a dictionary key that is having an empty space between words such as 'this fails' doesn't work:
{{ test.this fails }}
I get this error:
Could not parse the remainder:
' fails'
from'this fails'
How can I overcome this problem? I am not the one filling the models, so I can't change the keys of the dict to remove spaces.
解决方案The filter you want is something like
@register.filter(name='getkey') def getkey(value, arg): return value[arg]
And used with
{{test|getkey:'this works'}}
source: http://www.bhphp.com/blog4.php/2009/08/17/django-templates-and-dictionaries
这篇关于具有Django模板空间的dict键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!