Streaming a CSV file in Django

后端 未结 3 1689
感动是毒
感动是毒 2020-12-08 05:15

I am attempting to stream a csv file as an attachment download. The CSV files are getting to be 4MB in size or more, and I need a way for the user to actively download the f

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 05:59

    The problem I was having was with the ConditionalGetMiddleware. I saw django-piston come up with a replacement middleware for the ConditionalGetMiddleware that allows streaming:

    from django.middleware.http import ConditionalGetMiddleware
    
    def compat_middleware_factory(klass):
        """
        Class wrapper that only executes `process_response`
        if `streaming` is not set on the `HttpResponse` object.
        Django has a bad habbit of looking at the content,
        which will prematurely exhaust the data source if we're
        using generators or buffers.
        """
        class compatwrapper(klass):
            def process_response(self, req, resp):
                if not hasattr(resp, 'streaming'):
                    return klass.process_response(self, req, resp)
                return resp
        return compatwrapper
    
    ConditionalMiddlewareCompatProxy = compat_middleware_factory(ConditionalGetMiddleware)
    

    So then you will replace ConditionalGetMiddleware with your ConditionalMiddlewareCompatProxy middleware, and in your view (borrowed code from a clever answer to this question):

    def csv_view(request):
        def data():
            for i in xrange(10):
                csvfile = StringIO.StringIO()
                csvwriter = csv.writer(csvfile)
                csvwriter.writerow([i,"a","b","c"])
                yield csvfile.getvalue()
    
        #create the reponse object with a csv mimetype
        response = HttpResponse(
            data(),
            mimetype='text/csv',
            )
        #Set the response as an attachment with a filename
        response['Content-Disposition'] = "attachment; filename=test.csv"
        response.streaming = True
        return response
    

提交回复
热议问题