tags:

views:

558

answers:

4

Dear fellow users,

recently I wanted to return through a Django view a dynamically generated XML tree. The module I use for XML manipulation is the usual cElementTree.

I think I tackled what I wanted by doing the following:

def view1(request):
    resp = HttpResponse(g())
    return resp

def g():
     root = Element("ist")
     list_stamp = SubElement(root, "list_timestamp")
     list_creation = str(datetime.now())

     for i in range(1,1000000):
         root.text = str(i)
         yield cET.tostring(root)

Is something like this a good idea ? Do I miss something ?

+2  A: 

Does it work? If it doesn't work, what error does it throw?

If you're building a full-blown API for a django site, take a look at django-piston. It takes care of a lot of the busywork related to that.

http://bitbucket.org/jespern/django-piston/wiki/Home

Paul McMillan
It doesn't throw any error. And I think it works just fine. I just wanted a 2nd opinion. Unfortuantely, it's not a restful api. Just a view serializing thousands of objects in one go.
Andrew Brown
+2  A: 

Yes, it's perfectly legitimate to return an iterator in an HttpResponse. As you've discovered, that allows you to stream content to the client.

Daniel Roseman
I was worried about a remark in django-piston website that mentions: * Django's support for streaming breaks with ConditionalGetMiddleware and CommonMiddleware. I'm worried about any unseen repercussions.
Andrew Brown
+1  A: 

Yes. That's THE WAY you do it on Django.

Juanjo Conti
+6  A: 

About middlewares "breaking" streaming:

CommonMiddleware will try to consume the whole iterator if you set USE_ETAGS = True in settings. But in modern Django (1.1) there's a better way to do conditional get than CommonMiddleware + ConditionalGetMiddleware -- condition decorator. Use that and your streaming will stream okay :-)

Another thing that will try to consume the iterator is GzipMiddleware. If you want to use it you can avoid gzipping your streaming responses by turning it into a decorator and applying to individual views instead of globally.

isagalaev