Python XML和Django问题(与unicode相关?)
我制作了这个应用程序,它应该提供一个包含5项的xml文档。My view将包含以下五项的字典“rsscontent”发送到文件rss.xml:Python XML和Django问题(与unicode相关?),python,xml,django,unicode,Python,Xml,Django,Unicode,我制作了这个应用程序,它应该提供一个包含5项的xml文档。My view将包含以下五项的字典“rsscontent”发送到文件rss.xml: #views.py return render_to_response("rss.xml", {"rsscontent":rsscontent}) 然后,rss.xml文件如下所示: #rss.xml <?xml version="1.0" encoding="UTF-8"?> <rss version="0.92">
#views.py
return render_to_response("rss.xml", {"rsscontent":rsscontent})
然后,rss.xml文件如下所示:
#rss.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss version="0.92">
<channel>
{% for key, value in rsscontent.items %}<item>
<title>{{value|safe}}</title>
<description>{{value|safe}}</description>
<link>{{key|safe}}</link>
</item>
{% endfor %}
</channel>
</rss>
<title>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</title>
<description>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</description>
{'item1':'itemtext',
'item2':'item2text',
etc.
}
<title>{{ value.0 }}</title>
#rss.xml
{键为%,rsscontent.items%中的值为}
{{value | safe}}
{{value | safe}}
{{key | safe}}
{%endfor%}
它生成一个输出,但是页面的行为很奇怪。输出中有[u'],如下所示:
#rss.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss version="0.92">
<channel>
{% for key, value in rsscontent.items %}<item>
<title>{{value|safe}}</title>
<description>{{value|safe}}</description>
<link>{{key|safe}}</link>
</item>
{% endfor %}
</channel>
</rss>
<title>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</title>
<description>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</description>
{'item1':'itemtext',
'item2':'item2text',
etc.
}
<title>{{ value.0 }}</title>
[u'Apple iPod touch 8 GB(第四代)最新型号]
[u'苹果iPodtouch 8 GB(第四代)最新型号']
如何删除此内容?并确保它是正常文本?不要使用
|safe
过滤器。此外,看起来对象值是列表,因此需要获取其中的第一个元素,而不仅仅是打印项目本身
您希望修改输入rsscontent
变量,使其如下所示:
#rss.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss version="0.92">
<channel>
{% for key, value in rsscontent.items %}<item>
<title>{{value|safe}}</title>
<description>{{value|safe}}</description>
<link>{{key|safe}}</link>
</item>
{% endfor %}
</channel>
</rss>
<title>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</title>
<description>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</description>
{'item1':'itemtext',
'item2':'item2text',
etc.
}
<title>{{ value.0 }}</title>
而不是你目前的工作可能有:
{'item1':['itemtext',],
'item2':['item2text',],
etc.
}
如果不能这样做,则需要修改模板值输出,如下所示:
#rss.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss version="0.92">
<channel>
{% for key, value in rsscontent.items %}<item>
<title>{{value|safe}}</title>
<description>{{value|safe}}</description>
<link>{{key|safe}}</link>
</item>
{% endfor %}
</channel>
</rss>
<title>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</title>
<description>[u'Apple iPod touch 8 GB (4th Generation) NEWEST MODEL']</description>
{'item1':'itemtext',
'item2':'item2text',
etc.
}
<title>{{ value.0 }}</title>
{{value.0}
这将获取列表中的第一项,而不是打印列表本身。谢谢Paul!我不能丢失|保险箱,因为我会遇到另一个问题,然而,value.0方法对我有效。我不明白为什么这是一个listobject,所以我使用了这个方法,再次感谢!很乐意帮忙!小心使用
| safe
,这是一种让网站不安全的好方法。