为了账号安全,请及时绑定邮箱和手机立即绑定

如何对 html_string 进行 base64 编码

如何对 html_string 进行 base64 编码

杨__羊羊 2021-08-14 16:47:09
目的我正在尝试将foliumchoropleth编码为StringIO. 我基于我对相关查询的回答。我已经在这里和这里检查了答案。错误AttributeError: 'bytes' object has no attribute 'encode'代码视图.pydef get_choropleth(self, request):    # make choropleth ('m')    html_string = m.get_root().render()    f = StringIO(html_string)    choropleth = base64.b64decode(f.read())    choropleth = choropleth.encode('utf8') # causing error    return {'choropleth':choropleth}
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

经过一些反复试验,以下对我有用:


解决方案

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}


查看完整回答
反对 回复 2021-08-14
  • 1 回答
  • 0 关注
  • 300 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号