python - Django non-ASCII chars in template tags values -


i'm writing custom django template tag french application. template tag takes parameter string:

{% mytag "hello" %} 

is works perfect, fails when trying put non-ascii chars in value.

how thing work:

{% mytag "Êtes-vous à paris ?" %} 

i got error:

'ascii' codec can't encode character u'\xca' in position 0: ordinal not in range(128)

unicode error hint

the string not encoded/decoded was: Êtes-v

thanks in advance!

edit: python version 2.7. here code of tag:

@register.simple_tag(takes_context=true) def mytag(context, my_var):     return "here is: {my_var}".format(my_var=my_var) 

try replacing

return "here is: {my_var}".format(my_var=my_var) 

by

return u"here is: {my_var}".format(my_var=my_var) 

in python 2.7, "here is: {my_var}" str object, encoded string, my_var unicode object, decoded string, when formatting, python try encode my_var matches type of formatting string. using ascii codec default not support special characters.

adding u before formatting string makes unicode string, no encoding take place during formatting.

since looks might speak french, advise read this article great guide encoding in python.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -