django模板中的字符串替换

时间:2013-06-17 03:50:37

标签: django django-templates string-formatting

如何在django的默认模板引擎中实现字符串替换?我试图用stringformat基本实现"hello %s" % "world"。我似乎无法做到这一点。到目前为止,我最好的尝试{{ "hello %s"|stringformat:"world" }}没有输出。

1 个答案:

答案 0 :(得分:1)

stringformat:E中,E表示conversion types,在这种情况下为Floating Point Exponential Format。这里,"world"不是有效的转换类型,因此失败。

这不可能,因为templatetag方法中的参数必须是context variablestringformat的想法是convert types,而不是按照您希望的方式格式化字符串。