【问题标题】:How to change dynamically attachment file name in django CSV export?如何在 django CSV 导出中动态更改附件文件名?
【发布时间】:2019-12-19 11:19:48
【问题描述】:

我添加了静态文件名 manju.csv 但我想将变量分配给像今天日期这样的文件名。 请帮帮我

def district_wise_download(request):
    from_date = request.GET.get('from_date')
    to_date = request.GET.get('to_date')
    district = request.GET.get('district')
    print(district)
    if 'district' in request.GET:

        new_from = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
        new_to = datetime.datetime.strptime(to_date, '%Y-%m-%d').date()
        min_dt = datetime.datetime.combine(new_from, datetime.time.min)
        max_dt = datetime.datetime.combine(new_to, datetime.time.max)

        download_district = All_enquiries.objects.filter(Q(enquired_at__range = (min_dt, max_dt))&Q(district=district))

        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] =  'attachment; filename="manju.csv"'

        writer =  csv.writer(response, delimiter=',')
        writer.writerow(['created_at','product_name','product_category','price','customer_name','customer_mobile','state','district','city','pincode','status','remarks','source','username'])

        for obj in download_district:
           writer.writerow([obj.enquired_at,obj.product_name,obj.product_category,obj.price,obj.customer_name,obj.customer_mobile,obj.state,obj.district,obj.city,obj.pincode,obj.status,obj.remarks,obj.get_source_display(),obj.user_id])

        return response

【问题讨论】:

  • 这个问题回答的比较频繁,下次请google。首先在文件顶部导入日期时间在此处添加format 函数,然后在response['Content-Disposition'] = 'attachment; filename="manju_{}.csv".format(datetime.date.today())"
  • 非常感谢您

标签: python django csv


【解决方案1】:

您需要从datetime 导入date 模块。

from datetime import date
def district_wise_download(request):
    from_date = request.GET.get('from_date')
    to_date = request.GET.get('to_date')
    district = request.GET.get('district')
    print(district)
    if 'district' in request.GET:

        new_from = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
        new_to = datetime.datetime.strptime(to_date, '%Y-%m-%d').date()
        min_dt = datetime.datetime.combine(new_from, datetime.time.min)
        max_dt = datetime.datetime.combine(new_to, datetime.time.max)

        download_district = All_enquiries.objects.filter(Q(enquired_at__range = (min_dt, max_dt))&Q(district=district))

        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="manju_{}.csv"'.format(date.today())

        writer =  csv.writer(response, delimiter=',')
        writer.writerow(['created_at','product_name','product_category','price','customer_name','customer_mobile','state','district','city','pincode','status','remarks','source','username'])

        for obj in download_district:
           writer.writerow([obj.enquired_at,obj.product_name,obj.product_category,obj.price,obj.customer_name,obj.customer_mobile,obj.state,obj.district,obj.city,obj.pincode,obj.status,obj.remarks,obj.get_source_display(),obj.user_id])

        return response

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2016-12-04
    • 2019-08-19
    • 2022-01-23
    • 2018-04-09
    • 2020-02-24
    • 2018-10-28
    • 2017-07-18
    相关资源
    最近更新 更多