目的我正在嘗試將foliumchoropleth編碼為StringIO. 我基于我對相關(guān)查詢的回答。我已經(jīng)在這里和這里檢查了答案。錯誤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條經(jīng)驗 獲得超8個贊
經(jīng)過一些反復(fù)試驗,以下對我有用:
解決方案
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}
添加回答
舉報
0/150
提交
取消