【问题标题】:Convert int to ascii [chr()] in django template在 django 模板中将 int 转换为 ascii [chr()]
【发布时间】:2011-08-31 23:30:07
【问题描述】:

我正在编写一个 django 应用程序,并且在一个循环中,

    {% for item in list %}
    {{ forloop.counter0 }}
    {% endfor %}

这将打印从0开始的循环中的数字。但是我想打印从'A'开始的字母,所以python的方法是chr(forloop.counter0 + 65),但这是在模板内, 有任何想法吗?谢谢。

【问题讨论】:

    标签: python django django-templates ascii chr


    【解决方案1】:

    你可以写一个简单的custom template tag,例如一个过滤器:

    @register.filter(name='chr')
    def chr_(value):
        return chr(value + 65)
    

    然后将其加载到您的模板中即可:

    {{ forloop.counter0|chr }}
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多