开发者

How do I generate an XML file with the proper CGI header in pylons?

开发者 https://www.devze.com 2023-02-22 12:56 出处:网络
I\'m trying to write a simple pylons action to generate some xml.Here\'s the gist, with toy xml: class HelloController(BaseController):

I'm trying to write a simple pylons action to generate some xml. Here's the gist, with toy xml:

class HelloController(BaseController):
    def i开发者_开发百科ndex(self):
        my_xml = etree.Element("root")
        etree.SubElement(root, "child1")
        etree.SubElement(root, "child2")
        etree.SubElement(root, "child3")

        return '<?xml version="1.0" encoding="UTF-8"?>\n'+etree.tostring( my_xml, pretty_print=True )

But when I display this in firefox, it's a garbled mess--firefox doesn't try to parse it as xml.

The problem is (I think) that I'm not attaching the proper CGI header, but I can't find any documentation on how to do that in pylons. What should I do?

thanks!


You need to return response HTTP header that declares content type of the response. In a Pylons controller you use global pylons.response (which is an instance of webob.Response) object for that.

At the imports section add:

from pylons import response

Somewhere in your controller action add:

response.content_type = "text/xml"
0

精彩评论

暂无评论...
验证码 换一张
取 消