Jinja有一个"中心"格式化选项,但"右对齐"怎么样?

Rod*_*Day 6 python jinja2

说我有

{% for key,value in adict %}
{{key}}:{{value}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

如何确保填充所有键以使输出为

     something: 1
someotherthing: 3
  thelastthing: 2
Run Code Online (Sandbox Code Playgroud)

编辑:这不是我正在处理的网页,我只是获取打印的字符串输出.

Rod*_*Day 11

{{ key.rjust(20) }}:{{value}} 做了伎俩

我不知道你可以从框中调用python字符串命令.如果某人有更多的"jinja"解决方案,使用管道,我会给出答案.


小智 6

使用名为format 的内置 Jinja2 过滤器。例如:

宽度为 20 的左对齐字符串:

{{ "%-20s"|format(variable) }} 
Run Code Online (Sandbox Code Playgroud)

宽度为 20 的右对齐字符串:

{{ "%20s"|format(variable) }}
Run Code Online (Sandbox Code Playgroud)

您的案例:

{{ "%20s:%s"|format(key, value) }}
Run Code Online (Sandbox Code Playgroud)