gpt4 book ai didi

python - 将生成的 CSV 文件附加到电子邮件并使用 Django 发送

转载 作者:IT老高 更新时间:2023-10-28 21:00:06 26 4
gpt4 key购买 nike

我需要根据查询集结果生成一个 csv 文件,将生成的文件作为附件附加到电子邮件中并发送。如您所见,我需要遍历assigned_leads 并将其写入文件,因此我认为yield 可以解决问题。现在,当我运行代码时,我会收到带有附件的电子邮件,其中包含以下消息,而不是我期望的行。如果我使用 return,我会从查询集结果中获取一行。

<generator object data at 0x7f5e508d93c0>

def send_lead_reminder(request):
usercompany = Listing.objects.filter(submitted_by=request.user)
assigned_leads = lead.objects.filter(assigned_to__in=usercompany).distinct()
def data():
csvfile=StringIO.StringIO()
csvwriter =csv.writer(csvfile)
for leads in assigned_leads:
csvwriter.writerow([leads.business_name, leads.first_name, leads.last_name, leads.email, leads.phone_number,leads.address, leads.city, leads.state, leads.zipcode, leads.submission_date, leads.time_frame, leads.comments])
yield csvfile.getvalue()
message = EmailMessage("Hello","Your Leads","myemail@gmail.com",["myemail@gmail.com"])
message.attach('invoice.csv', data(), 'text/csv')
#message.to="myemail@gmail.com"
message.send()
return HttpResponseRedirect('/')

最佳答案

您使用附加功能有什么特别的原因吗?只需在内存中构建您的 csv - 如果您将其附加到电子邮件中,您将无法避免这种情况 - 然后发送。

assigned_leads = lead.objects.filter(assigned_to__in=usercompany).distinct()
csvfile = StringIO.StringIO()
csvwriter = csv.writer(csvfile)
for leads in assigned_leads:
csvwriter.writerow([leads.business_name, leads.first_name, leads.last_name, leads.email, leads.phone_number,leads.address, leads.city, leads.state, leads.zipcode, leads.submission_date, leads.time_frame, leads.comments])
message = EmailMessage("Hello","Your Leads","myemail@gmail.com",["myemail@gmail.com"])
message.attach('invoice.csv', csvfile.getvalue(), 'text/csv')

关于python - 将生成的 CSV 文件附加到电子邮件并使用 Django 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17584550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com