hi i am using jinja2 in google app engine for rendering template but when im passinf arabic or persian string az template variable i get this error
فروشگاه {{ name }}
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 0: ordinal not in range(128)
below i've tried to encode it so that it would be jinja acceptable but the string doesn't appear at all
def deccode(n):
n = n.decode("utf-8")
n = n.encode("ascii","ignore")
return n
name = 'رشد'
name = deccode(name)
logo = 'roshd'
logo = deccode(logo)
ss = {'name': name, 'logo': logo}
s = template.render(ss)
<div class=" title">
<i class="dropdown icon"></i>
فروشگاه
so whats the best way to pass arabic to jinja 2 ?
Make sure to pass a unicode string to the template. Assuming you are on Python 2 this means prefixing the string literal with u
:
name = u'رشد'
Also, get rid of your custom decode function. It's not needed. Make sure to save the file as UTF-8 though and add a comment in the first line of the file indicating the encoding of the file as mentioned in @manikandan's answer and PEP 263