简体   繁体   English

在django模板中将int转换为ascii [chr()]

[英]Convert int to ascii [chr()] in django template

I am writing a django application, and in a loop, 我正在写一个django应用程序,并在循环中,

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

this will printout the number in the loop starting from 0. But I want to printout alphabet starting from 'A', so the python way to do it is chr(forloop.counter0+65), but this is inside the template, any ideas? 这将打印输出从0开始的循环中的数字。但是我想从'A'开始打印输出字母,所以python方式是chr(forloop.counter0 + 65),但这是在模板里面,任何想法? thanks. 谢谢。

You can write a simple custom template tag , for example a filter: 您可以编写一个简单的自定义模板标记 ,例如过滤器:

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

Then load it in your template and you can do: 然后将其加载到模板中,您可以执行以下操作:

{{ forloop.counter0|chr }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM