python - Django 请求 XML 文件

标签 python xml django

您好,我正在尝试将 XML 文件从一个 View 发送到另一个 View 。

第一个 View (在此 View 中我发送 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)

第二个 View (在此 View 中我应该阅读 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")

这样,在view2中我读取了文件/path/to/respuesta_error.xml,我不知道应该做什么来读取view1中通过POST发送的XML文件。

我尝试使用 request 和 request.body 但不起作用。

谢谢

最佳答案

我发现 request.body 包含字符串形式的 XML,因此 view2 应该如下所示:

@csrf_exempt
# https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element
def view2(request):
    text = strip_ns(request.body)

    file_new = open("ultima_respuesta.xml", "w")
    file_new.write(texto)
    file_new.close()

    return HttpResponse("test")

关于python - Django 请求 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24912293/

相关文章:

python - 按数据帧分组 : Use column-values in current and previous row in Function

Python:通过 iter() 在 ElementTree 中使用命名空间

java - 如何让深色模式在我的应用程序上运行?

python - 调试 uWSGI worker 正在做什么

python - python进程/线程映射在Windows中如何工作?为什么线程比进程运行得更快?

python - 在 Pandas 中如何引用 2 个数据框并替换列

java - 如何让我的 Android 应用程序方法减少 RAM 消耗

python - 如何使用测试数据库响应 django.test.Client

mysql - 每个帖子的单独获取请求和数据库命中以获得类似状态

python - 如何删除文本列中的微小变化