0

我正在尝试打开一个 xml 文件,操作并呈现响应或从 url 下载文件。我正在返回多个对象,当我在 for 循环之后说 print 时,在终端中,我看到每个请求的对象都来了,但是当我说返回时,只有单个对象同时到达请求的 url 和终端。这是我的代码;

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
4

1 回答 1

2

一旦函数“返回”,它就会终止。因此,您的 for 循环只执行一次。你可能想用'yield'替换'return',这样你的函数就变成了一个生成器,在for循环的每次迭代中都会产生一个响应。

于 2012-09-01T10:25:27.937 回答