Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以在 django 模板中下标字符串?
我的意思是在做什么
print "hello"[:3] => hel
在Django 模板中。
写作{{ stringVar[:10] }}投掷
{{ stringVar[:10] }}
TemplateSyntaxError: Could not parse the remainder: '[:10]' from 'stringVar[:10]'
可以,但语法与您习惯的略有不同。使用切片:
{{ stringVar|slice:":2" }}
这将为您提供列表中的前 2 个元素(或字符串中的前两个字符)。