【发布时间】: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())" -
非常感谢您