I have a function in a view class of my content type that returns a dictionary. The dictionary contains values retrieved from a mysql backend database (uses UTF-8 encoding).
One of the fields contains a special character (a combined with e), but renders as a "diamond with a question mark".
Here is how I build the dictionary I return in the function:
assetDetails = Session.execute("CALL getAssetDetails("+self.assetID+");").fetchone()
return dict(assetID=assetDetails[0],
gpclAssetID=assetDetails[1],
assetType=assetDetails[2],
assetDescription=assetDetails[3],
supplierName=assetDetails[4],
model=assetDetails[5],
serialNumber=assetDetails[6],
status=assetDetails[7],
location=assetDetails[8],
options=assetDetails[9])
In my template file, the html tag is:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
lang="en"
metal:use-macro="context/main_template/macros/master"
i18n:domain="gpcl.assets">
I show the information in this div:
<div tal:define="asset view/getAssetDetails">
<div tal:content="asset/assetID"></div>
<div tal:content="asset/gpclAssetID"></div>
<div tal:content="asset/assetType"></div>
<div tal:content="asset/assetDescription"></div>
<div tal:content="asset/supplierName"></div>
<div tal:content="asset/model"></div>
<div tal:content="asset/serialNumber"></div>
<div tal:content="asset/status"></div>
<div tal:content="asset/location"></div>
<div tal:content="asset/options"></div>
</div>
How do I make it so that special characters will display properly?
Thank you in advance.
i'd convert your data to unicode to be on the safe side:
from Products.CMFPlone.utils import safe_unicode
...
return dict(assetID=safe_unicode(assetDetails[0]),
gpclAssetID=safe_unicode(assetDetails[1]),
assetType=safe_unicode(assetDetails[2]),
...
safe_unicode by default uses 'uft-8' but can be told to use a different character set for decoding your string: safe_unicode('some text', 'latin-1')
If your strings are really 8 bit strings in UTF-8 encoding, you can simply use the Python .decode
function.
<div tal:define="asset view/getAssetDetails">
<div tal:content="python:asset.assetID.decode('utf-8')"></div>
...
See https://docs.python.org/2/howto/unicode.html for more.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.