django django模板中的输出呈现为{'__ unicode __:{}}

时间:2015-05-21 16:55:55

标签: python django unicode django-templates

我正在使用django模板创建一个xml,它将我的一些变量(所有这些都来自python字典)输出为{'__unicode__': {} 但不是全部。 / p>

我看不到变量被破坏的模式....

这种情况发生的一般原因是什么?

以下是我正在运行的代码的粗略概念:

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>{&#39;__unicode__&#39;: {}</MySecondTag>
</ParentTag>

所以我不知道它是addict还是别的什么。该软件包中的任何内容似乎都没有使用unicode,并且它在python 2.7上进行了“测试”。这在我的python解释器中不会发生。

1 个答案:

答案 0 :(得分:0)

这是addict发送空字典的方式。 It's broken

这只是意味着您正在尝试访问不存在的字典值。