Yibing Liu Yibing Liu - 11 months ago 39
HTML Question

{{=XML(some thing)}} can't be parsed in html with web2py

I am using magicsuggest as a auto-complete plugin of a web application with web2py. I define a list variable

in the model/db.py. The element in the list is Chinese. However when I embeded the variable in the html like
according to the manual book of magicsuggest. The chinese character was garbled. After several days searching, I find the list variable with chinese character was encode into hex in the html. I know there is something wrong about encode/decode. Could someone help me to display the correct chinese character in the html?

Answer Source

XML() is meant to take a string, not a list of strings. If you pass it something other than a string, it will first be converted to a string, so your code is equivalent to {{=XML(str(dt))}}, and you'll notice that in Python, str(['张','李']) yields "['\\xe5\\xbc\\xa0', '\\xe6\\x9d\\x8e']".

Instead, you can do {{=XML(dt[0])}}, and you will see the first character in the list displayed properly.

If you want to display a comma separated list surrounded by brackets, you can do:

{{=json.dumps(dt, encoding="UTF-8", ensure_ascii=False)}}