Mikhail Nikashov Mikhail Nikashov - 1 month ago 10
PHP Question

Generate custom xml with django

As a heads up i am completely new to Python and Django.

I am trying to move from PHP to Python. And i came to a problem of how to generate a custom xml file with all of the entries form database. I need to create something like this:





<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

For those wondering here is the final code for the saving of the XML There is obviously got to be a better way but this is what i came up with.

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)

Answer
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)