0

How can I download a large CSV file that shows me a 502 bad gateway error? I get this solution I added in below. Actually, in this, we use streaming references. In this concept for example we download a movie it's will download in the browser and show status when complete this will give the option to show in a folder same as that CSV file download completely this will show us.

Akash Nagtilak
  • 325
  • 1
  • 5

1 Answers1

0

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)

Akash Nagtilak
  • 325
  • 1
  • 5