1
嗨我試圖發送一個XML文件從一個視圖到另一個。Django請求的XML文件
第一視圖(在該視圖我發送XML):
def view1(request):
xml_file = open("/path/to/respuesta_error.xml", "rb").read()
r = urllib2.Request("http://localhost:8000/ingram/response/", data=xml_file, headers={'Content-Type': 'text/xml'})
u = urllib2.urlopen(r)
response = u.read()
return HttpResponse(response)
第二視圖(在該視圖中,我應該讀取XML):
@csrf_exempt
# https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element
def view2(request):
xml_file = open("/path/to/respuesta_error.xml", "rb") # <-- How can I read the request XML?
text = xml_file.read()
text = strip_ns(text)
file_new = open("ultima_respuesta.xml", "w")
file_new.write(texto)
file_new.close()
return HttpResponse("test")
就這樣,在視圖2我讀文件/path/to/respuesta_error.xml,我不知道該怎麼做才能讀取由view1發送的POST文件。
我嘗試使用請求和request.body但不起作用。
感謝
你也可以使用'request.read() '?此外,由於您的方法是POST,您可能還需要檢查它。默認情況下,視圖地址爲「GET」方法AFAIK。 – sagarchalise
Okey,謝謝我將檢查該方法是否爲POST。爲什麼更好地使用request.read()? –