简体   繁体   English

Jinja2:将十六进制转换为Base64

[英]Jinja2: Converting hex to Base64

Is this possible? 这可能吗?

Say I have a hex value 32ABE23 , is there a filter to show it as Base64? 说我有一个十六进制值32ABE23 ,是否有一个过滤器将其显示为Base64? Well I saw that there is | 好吧,我看到有 b64encode filter. b64encode过滤器。

I trried following with no success 我尝试跟随但没有成功

{{ 32ABE23 | string() | b64encode }}

I get 我懂了

TemplateAssertionError: no filter named 'b64encode'

You can define your own filters in Jinja2: 您可以在Jinja2中定义自己的过滤器:

import base64
def b64encode(s):
    return base64.b64encode(s)

jinja2_environment.filters['b64encode'] = b64encode

Of course this can be simplified as: 当然,这可以简化为:

import base64
jinja2_environment.filters['b64encode'] = base64.b64encode

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

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