Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

UnicodeDecodeError with Jinja2 + GAE

I get the following error when trying to render foreign chars from a dict. Do you have any idea what I should do?

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)

Here is my variable:

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,
    }}

I will try this with some hope that it will succeed:

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,
    }}

Upvotes: 0

Views: 625

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101139

Your location_map_br_11_cap variable contains values that have names which are raw strings rather than unicode strings. Convert your strings to Unicode, so Jinja knows how to encode them when it generates the template.

Upvotes: 4

Related Questions