youxun
youxun

Reputation: 21

When using mako template rendering html page, if there is utf-8 character,it's will tell me error

The error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128. 

The code:

lookup = mako.lookup.TemplateLookup(directories=[template_path] , input_encoding="utf-8", output_encoding="utf-8", encoding_errors="replace" ) 
template = lookup.get_template(filename) 
template.render(**kwargs)

Have to manually cast to utf-8 characters can, which is mako birth defects?

Upvotes: 0

Views: 432

Answers (1)

MobilePro.pl
MobilePro.pl

Reputation: 341

I had similar problem, when I used translation functionality with i18n and I had in template file string to be translated with national characters, but without u sign, for example:

${'polish word with national character: mózg'}

it raised similar exception, you should escape string with 'u' character:

${u'polish word with national character: mózg'}

You can also try to add coding header on the top of your template file:

## -*- coding: utf-8 -*-

I hope it will solve your problem.

Upvotes: 1

Related Questions