0

我正在同时学习 Python 和 django。我正在尝试创建一个 xml 文档以从视图中返回一些 XML。我目前正在使用 django 开发服务器,并且我不断在我的视图中得到这些信息,而不是我试图创建的文档。

这是我的代码

    from django.http import HttpResponse
    from mypoject.myapp.models import Username
    from django.core import serializers
    from xml.dom.minidom import Document
    import datetime


    def authenticate(request, username):
        if request.method == "GET":

            #Try to get the username
            try:
                checkUser = Username.objects.get(username__exact = username)
                user = userCheck.get(username__exact = username)
                userXML = serializers.serialize("xml", checkUser)

            except Username.DoesNotExist:
                #return XML with status "Failed"
                return HttpResponse(xml, mimetype="text/xml")       
            except:
                #return XML with status "Failed"

                xmlFailed = Document()

                meta = xmlFailed.createElement("meta")
                xmlFailed.appendChild(meta)

                status = xmlFailed.createElement("status")
                meta.appendChild(status)
                statusText = xmlFailed.createTextNode("Failed")
                status.appendChild(statusText)

                message = xmlFailed.createElement("message")
                meta.appendChild(message)

                totalRecords = xmlFailed.createElement("totalRecords")
                meta.appendChild(totalRecords)

                executionTime = xmlFailed.createElement("executionTime")
                meta.appendChild(executionTime)

                return HttpResponse(xmlFailed, mimetype="text/xml")
            else:
                #return happy XML code with status "Success"

当我在浏览器中查看它时,这就是屏幕上的内容......

<xml.dom.minidom.Document instance at 0x993192c>

如果我注释掉 Document() 创建消失。所以我想我只需要它不吐出信息。我一直在寻找,我找不到一个刻板的答案,这让我相信我错过了一些明显的东西。

谢谢你的帮助!

4

1 回答 1

1

You'll need to call xmlFailed.toxml() or the like in order to get XML out of your object -- looks like that's not what you're doing (in the code you didn't show us).

于 2009-06-16T03:49:18.657 回答