2016-08-02 3 views
0

Als Heads-Up bin ich völlig neu in Python und Django.Erstelle benutzerdefinierte XML mit Django

Ich versuche, von PHP nach Python zu verschieben. Und ich kam zu einem Problem, wie man eine benutzerdefinierte XML-Datei mit allen Einträgen der Formulardatenbank erzeugt. Ich brauche so etwas zu schaffen:

<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

Für diejenigen hier fragen, ist der letzte Code für die Speicherung der XML Es offensichtlich sein wird, bekam ein besserer weg, aber das ist, was ich mir ausgedacht habe.

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) 
+0

Was haben Sie versucht, so weit? Müssen Sie herausfinden, wie man einen String erstellt oder in eine Datei oder etwas anderes schreibt? – sixtytrees

+0

Hier ist was ich bisher habe. Ich erstelle ein benutzerdefiniertes XML von einer Vorlage, durch Ansichten und jetzt versuche ich, dieses XML in einer Datei zu speichern. –

Antwort

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