[英]Error When Using Django send_mail() Function
I want to send a mail to users that fill the email field in my django app. 我想向在django应用程序中填写电子邮件字段的用户发送邮件。 The process is like this, when a user fill the form, if it's valid, it will save in the database and pick the user email address from the database and send mail.
流程是这样的,当用户填写表格时,如果有效,它将保存在数据库中并从数据库中选择用户电子邮件地址并发送邮件。 I tried the below codes but I'm getting this error:
我尝试了以下代码,但出现此错误:
ValueError at /confirm/
need more than 1 value to unpack
Request Method: POST
Request URL: http://127.0.0.1:8000/confirm/
Django Version: 1.4
Exception Type: ValueError
Exception Value:
need more than 1 value to unpack
Exception Location: C:\Python27\lib\site-packages\django\core\mail\message.py in sanitize_address, line 102
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Models 楷模
class Invite(models.Model):
email_address=models.EmailField(max_length=75)
def __unicode__(self):
return self.email_address
Views 观看次数
def invite_me(request):
if request.method=="POST":
form=InviteForm(request.POST)
if form.is_valid():
form.save()
#get input data and send email to the user.
send_mail('Your Key Invite','Welcome to my world','test@gmail.com',
[Invite.objects.values('email_address')])
return HttpResponse('Thanks For Inputting Your Email, Go Check Your Email For Our Invite!')
else:
return HttpResponse('Invalid Email Address')
else:
form=InviteForm()
return render_to_response('home.html',{'InviteForm':InviteForm},context_instance=RequestContext(request))
Think about what Invite.objects.values('email_address')
returns? 考虑一下
Invite.objects.values('email_address')
返回什么? Definitely not what the send_mail
function accepts (a list of email addresses). 绝对不是
send_mail
函数接受的内容(电子邮件地址列表)。
Invite.objects.values_list('email_address', flat=True)
is what you need. 您需要
Invite.objects.values_list('email_address', flat=True)
。 Oh, and remove the []
. 哦,删除
[]
。
send_mail('foo', 'bar', 'baz@example.com', Invite.objects.values_list(...))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.