Dajngo CSV 文件无法下载?当我们下载一个大的 CSV 文件时,它需要一些时间? Django 502 bad gateway nginx error Django

发布于 2025-01-09 12:36:54 字数 150 浏览 0 评论 0原文

如何下载显示 502 bad gateway 错误的大型 CSV 文件? 我得到了下面添加的解决方案。 实际上,在这里,我们使用流引用。在此概念中,例如,我们下载一部电影,它将在浏览器中下载并在完成后显示状态,这将提供在与 CSV 文件下载完全相同的文件夹中显示的选项,这将向我们显示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2025-01-16 12:36:54

有一种解决方案可以解决此错误,即增加 nginx 时间,但这会影响成本,因此这是使用 Django 流的更好方法。流媒体就像一个例子,当我们添加一部电影以供下载时,它会在浏览器上下载。这个概念用在 Django 流中。

在 Django 中为此编写 View。
view.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')

请使用以下链接作为参考。
https://docs.djangoproject.com /en/4.0/howto/outputting-csv/#streaming-large-csv-files

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

GIT
手动将行添加到 StreamingHttpResponse (Django)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文