我正在尝试在 HTML 模板中从我的 view.py 中呈现字典,例如:
test = { 'works': True, 'this fails':False }
在模板中:
这没有问题:
{{ test.works }}
但是在单词之间有空格的字典键(例如“this fails”)不起作用:
{{ test.this fails }}
我收到这个错误:
Could not parse the remainder:
' fails'
from'this fails'
我该如何克服这个问题?我不是填充模型的人,所以我无法更改字典的键以删除空格。
最佳答案
你想要的过滤器是这样的
@register.filter(name='getkey')
def getkey(value, arg):
return value[arg]
与一起使用
{{test|getkey:'this works'}}
来源:http://www.bhphp.com/blog4.php/2009/08/17/django-templates-and-dictionaries
关于python - Django 模板中带空格的字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1906129/