作为一名新手,我对Python和Django完全陌生。
我正试图从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>
更新
对于那些想知道保存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)
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)