0

I am trying to open an xml file, manipulate and render to response or download file from url. I am doing to return multiple objects, when I say print after for loop, in terminal I see every requested objects comes but when I say return only single object comes both to requested url and to terminal.Here is my code;

def xml(request):
filename = "/usr/..../...."  
programs = x.objects.all()
categories = y.objects.all()

with open(filename,'r+') as f:  

    old = f.read()
    for m,k in itertools.product(categories,programs):
        if k.name_tr == m.name_tr:
            s = old.replace ('titlesss',k.name_tr,1) 
            j= k.introduction_tr
            decoded = BeautifulStoneSoup(j, convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
            x =str(decoded)
            x = unicode(x,"utf-8")
            s = s.replace ("infosss",x,1)
            if  m.id == 310:
                    s = s.replace('idsss',"231",1)
            elif m.id == 308:
                    s = s.replace ('idsss',"230",1)
            elif m.id == 159:
                    s = s.replace ('idsss',"203",1)
            elif m.id == 163:
                    s = s.replace ('idsss',"204",1)
            elif m.id == 280:
                    s = s.replace ('idsss',"212",1)
            elif m.id == 157:
                    s = s.replace ('idsss',"202",1)
            elif m.id == 282:
                    s = s.replace ('idsss',"211",1)
            response = HttpResponse(s,mimetype ="application/force-download")                                                       
            response['Content-Disposition'] = 'attachment; filename=output.xml'
            return response
tuna
  • 6,211
  • 11
  • 43
  • 63

1 Answers1

2

Once a function 'returns' it terminates. Therefore, your for loop only executes once. You might want to replace the 'return' with an 'yield', thus your function becoming a generator that yields a response at each iteration of the for loop.

Ioan Alexandru Cucu
  • 11,981
  • 6
  • 37
  • 39
  • when I add " yield s " I get 'generator' object has no attribute 'get'.Do you have any idea ? – tuna Sep 01 '12 at 11:45
  • Well, the problem is you are trying to do this in a django view, and django expects views are not generators ... Sorry, but my answer doesn't fully answer your question...If you want each new request to return a response using the next lines from programs and categories you would need to rely on some django session to preserve state between requests (like the current line you're currently looking at in each file). – Ioan Alexandru Cucu Sep 01 '12 at 12:59