如何使用base64对html字符串进行编码

2024-07-03 07:13:45 发布

您现在位置:Python中文网/ 问答频道 /正文

目的

我试图将foliumchoropleth编码为StringIO。我的答案是a related query。我已经检查了答案herehere。在

错误

AttributeError: 'bytes' object has no attribute 'encode'

代码

视图.py

^{pr2}$

Tags: no答案目的编码byteshereobject错误
1条回答
网友
1楼 · 发布于 2024-07-03 07:13:45

经过一番反复试验,以下几点对我有用:

解决方案

def get_choropleth(self, request):
        # make choropleth ('m')
        html_string = m.get_root().render()
        encoded_bytes = html_string.encode('utf-8')
        encoded_bytes = base64.b64encode(encoded_bytes)
        encoded_bytes = encoded_bytes.decode('utf8') # decode the b64 bytes for Unicode
        choropleth = encoded_bytes
        return {'choropleth':choropleth}

相关问题 更多 >