【发布时间】:2012-06-01 19:26:00
【问题描述】:
我的 settings.py 中有重音字符,我使用 getattr(settings, 'MY_CONSTANT_NAME', []) 在视图中访问这些字符,但 getattr() 调用返回损坏的字符(例如,“ô”变为:“\ xc3\xb4")。
这是 view.py 中的代码:
from django.conf import settings
def getValueFromSetting(request):
mimetype = 'application/json'
charset=utf-8' datasources = getattr(settings, 'MY_CONSTANT_NAME', [])
config= '{'
config+= '"datasources": ' + str(datasources).replace("'", '"')
config+= '}'
return HttpResponse(config,mimetype)
到目前为止我为解决问题所做的工作:
- 我把 # -- coding: utf-8 -- 作为我的 settings.py 和 my views.py 的第一行
- 我将 u'ô' 或 unicode('ô') 放在 settings.py 中特殊字符的前面
- 我将 DEFAULT_CHARSET = 'utf-8' 放在 settings.py 中
- 我尝试了所有可能的 .decode('utf-8')、.encode('utf-8')、.decode('iso-8859-1')、.encode('iso-8859-1 ') 在 settings.py 或 views.py 中的特殊字符上...
没有什么能解决问题。
有解决这个问题的建议吗?
谢谢
艾蒂安
【问题讨论】:
-
那么您究竟在哪里看到了这个“损坏”的输出?事实上,这对我来说看起来完全正确,但没关系。你是如何输出这个值的?
标签: django unicode encoding character-encoding