我有一个 boolean 值,它是假的,但我希望它在网页上打印时说“否”而不是“假”。
如果为真,打印“Yes”而不是“True”也是如此。我假设这是可能的!
模型中的代码:
urgent = models.BooleanField()
模板中的代码:
<p>Upvotes: {{ ticket.urgent}}</p>
现在,打印这个听起来很奇怪:
Urgent: False
但这听起来更自然、更好的用户体验等等:
Urgent: No
请您参考如下方法:
你可以只使用yesno
Maps values for True, False, and (optionally) None, to the strings “yes”, “no”, “maybe”, or a custom mapping passed as a comma-separated list, and returns one of those strings according to the value:
<p>Upvotes: {{ ticket.upvotes|yesno }}</p>
