0
作为一名高管,我对Python和Django完全陌生。使用django生成自定义xml
我想从PHP转移到Python。我来到一个问题,如何生成一个自定义XML文件与所有条目形式数据库。我需要创造这样的:
<inv>
<invID>1</invID>
<group>Group</group>
<name>Name</name>
<description></description>
</inv>
<inv>
<invID>2</invID>
<group>Group</group>
<name>Name</name>
<description></description>
</inv>
UPDATE
对于那些想知道这里是XML的节省最终代码有明显该杀更好的方式,但这是我想出的。
def xml(request):
#Getting all of the items in the Database
products = Product.objects.all()
#Putting all of it in to Context to pass to template
context = {
'products': products
}
#calling template with all of the information
content = render_to_string('catalog/xml_template.xml', context)
#Saving template tp a static folder so it can be accessible without calling view
with open (os.path.join(BASE_DIR, 'static/test.xml'), 'w') as xmlfile:
xmlfile.write(content.encode('utf8'))
#Not Sure if i actually need to call the return but i did not let me run it without content
return render(request, 'catalog/xml_template.xml', context)
你尝试过这么远吗?你需要找出如何建立一个字符串或写入文件或其他东西? – sixtytrees
这是我到目前为止。我从模板创建一个自定义XML,通过视图,现在我试图将该XML保存到文件。 –