尝试从dict渲染外来字符时出现以下错误。你知道我应该怎么做吗?
File "/base/data/home/apps/s~montaoproject/cap.354503891062679364/main.py", line 333, in render_jinja
self.response.out.write(template.render(data))
File "/base/data/home/apps/s~montaoproject/cap.354503891062679364/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "/base/data/home/apps/s~montaoproject/cap.354503891062679364/templates/list_jinja.html", line 199, in top-level template code
{% for key,value in location_map_br_11_cap.items() %}<option value="3" >{{ value.name }}</option>{% endfor %}
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 9: ordinal not in range(128)
这是我的变量:
location_map_br_11_cap = {1: {
'name': str('Toda Região 11 (ddd)'),
'lat': -23.55,
'long': -46.64,
'radius': 294200,
}, 2: {
'name': str('Todo Estado de São Paulo'),
'lat': -22.90,
'long': -43.21,
'radius': 294200,
}, 1: {
'name': str('Toda região Sudeste do Brasil'),
'lat': -23.55,
'long': -46.64,
'radius': 294200,
}}
我会尝试这一点,希望它会成功:
location_map_br_11_cap = {1: {
'name': str('Toda Região 11 (ddd)').decode('utf-8'),
'lat': -23.55,
'long': -46.64,
'radius': 294200,
}, 2: {
'name': str('Todo Estado de São Paulo').decode('utf-8'),
'lat': -22.90,
'long': -43.21,
'radius': 294200,
}, 1: {
'name': str('Toda região Sudeste do Brasil').decode('utf-8'),
'lat': -23.55,
'long': -46.64,
'radius': 294200,
}}
答案 0 :(得分:4)
您的location_map_br_11_cap
变量包含的名称是原始字符串而不是unicode字符串。将您的字符串转换为Unicode,因此Jinja知道如何在生成模板时对它们进行编码。