ValueError:需要多个值才能解压,Django电子邮件错误 [英] ValueError: need more than 1 value to unpack, django email error
问题描述
我正在尝试使用django同时向一群人发送电子邮件(尽管现在,我正在尝试使其仅与一个人一起使用).我有一个名为User
的类,该类具有一个字段email
和一个名为Group
的类的外键.现在,我正在尝试向特定组中的所有用户发送电子邮件.为此,我有以下代码:
I'm trying to use django to send emails to a bunch of people at the same time (although right now, I'm trying to get it to work with only one). I have a class called User
that has a field email
and a foreign-key to a class called Group
. Now, I'm trying to send an email to all users in a specific group. To do that, I have the following code:
addresses = User.objects.filter(group__group='Operations').values_list('email')
这可以正确获取电子邮件地址(如果我打印出addresses
,我会得到[(u'address@example.com',)]
.然后我使用addresses
创建电子邮件:
This is correctly getting the email addresses (if I print the addresses
I get [(u'address@example.com',)]
. I then use addresses
to create an email:
email = EmailMessage('Test',
get_template('test.html').render(Context({'content': 'This is a TEST!'})),
to = addresses)
当我之前进行此操作时,手动将电子邮件地址传递给"to"参数,它可以正常工作,但是现在它给我标题中的错误:"ValueError:需要多个值来解压.似乎会导致错误的代码行是email.send()
.
When I was doing this before, manually passing in an email address to the "to" argument, it worked perfectly, however now it gives me the error in the title: "ValueError: need more than 1 value to unpack. The line of code that seems to create the error is email.send()
.
此错误是什么意思?我为什么要得到它?我该如何解决?是否有更好的方法从所有具有指定组的用户中获取电子邮件地址?
What does this error mean? Why am I getting it? How should I fix it? Is there a better way to get the email addresses out of all the users with the specified group?
谢谢.
推荐答案
找到了解决方案.实际上很简单.
Found a solution. Very simple, actually.
addresses = User.objects.filter(group__group='Operations').values_list('email', flat=True)
这篇关于ValueError:需要多个值才能解压,Django电子邮件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!