2016-08-02 31 views
0

私はまったく新しく、PythonとDjangoを初めて使っています。djangoでカスタムxmlを生成

私はPHPからPythonに移行しようとしています。そして、私は、すべてのエントリフォームデータベースを持つカスタムXMLファイルを生成する方法の問題に遭遇しました。ここで疑問に思うことは、明らかがあるようになっている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

:私はこのようなものを作成する必要がありますより良い方法ですが、これは私が思いついたものです。

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

あなたは、これまで何を試してみましたか?文字列を作成する方法やファイルなどに書き込む方法を調べる必要がありますか? – sixtytrees

+0

ここまで私がこれまで持っていたことがあります。私はビューからテンプレートからカスタムXMLを作成していますが、今はそのXMLをファイルに保存しようとしています。 –

答えて

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) 
関連する問題