[Answered ]-Dajngo CSV FIle not download ? When we have a large CSV file download its takes some time?Django 502 bad gateway nginx error Django

1👍

There is one solution for resolving this error to increase nginx time but this is will affect cost so better way to use Django streaming. streaming is like an example when we add a movie for download it’s downloading on the browser. This concept is used in Django streaming.

Write View for this in Django.
views.py

from django.http import StreamingHttpResponse

503_ERROR = 'something went wrong.'
DASHBOARD_URL = 'path'

def get_headers():
   return ['field1', 'field2', 'field3']

def get_data(item):
   return {
        'field1': item.field1,
        'field2': item.field2,
        'field3': item.field3,
   }

class CSVBuffer(object):
def write(self, value):
    return value

class Streaming_CSV(generic.View):
   model = Model_name

   def get(self, request, *args, **kwargs):
       try:
           queryset = self.model.objects.filter(is_draft=False)
           response = StreamingHttpResponse(streaming_content=(iter_items(queryset, CSVBuffer())), content_type='text/csv', )
           file_name = 'Experience_data_%s' % (str(datetime.datetime.now()))
           response['Content-Disposition'] = 'attachment;filename=%s.csv' % (file_name)
       except Exception as e:
           print(e)
           messages.error(request, ERROR_503)
           return redirect(DASHBOARD_URL)
       return response

urls.py

path('streaming-csv/',views.Streaming_CSV.as_view(),name = 'streaming-csv')

For reference use the below links.
https://docs.djangoproject.com/en/4.0/howto/outputting-csv/#streaming-large-csv-files

GIT.
https://gist.github.com/niuware/ba19bbc0169039e89326e1599dba3a87

GIT
Adding rows manually to StreamingHttpResponse (Django)

Leave a comment