【发布时间】:2015-05-21 16:55:54
【问题描述】:
我正在使用 django 模板创建一个 xml,它会将我的一些变量(全部来自 python 字典)输出为 {'__unicode__': {}但不是全部。
我看不到变量被破坏的模式......
发生这种情况是否有一般原因?
这是我正在运行的代码的粗略概念:
from addict import Dict
def get_dictionary(self, orig_dict):
new_dict = Dict({'default_key':'default_value'})
if(orig_dict["code"]==0):
new_dict.other_key = "Double Quoted String"
elif(orig_dict["code"]==1):
new_dict.other_key = "Different Double Quoted String"
return new_dict
最终当我将它与render_to_string("xml_template.xml",dictionary=context_dictionary) 一起使用到此模板中时:
<ParentTag>
<MyFirstTag>{{new_dict.default_key}}</MyFirstTag>
<MySecondTag>{{new_dict.other_key}}</MySecondTag>
</ParentTag>
它呈现为:
<ParentTag>
<MyFirstTag>default_value</MyFirstTag>
<MySecondTag>{'__unicode__': {}</MySecondTag>
</ParentTag>
所以我不知道是addict,还是别的什么。该包中的任何内容似乎都没有使用 unicode,并且它在 python 2.7 上进行了“测试”。这在我的 python 解释器中不会发生。
【问题讨论】:
-
显示模板的相关部分,以及构建字典的代码。
-
@peterDeGlopper 我会尝试编辑一些内容,但我要回家自动取款机,无法访问我的代码。
-
似乎正在对撇号进行编码。
-
@karthkir 如果我有任何撇号,我会这么想。
-
@PeterDeGlopper 我明天会在工作中仔细检查我的代码。
标签: python django unicode django-templates