使用Django send_mail()函数时出错

时间:2012-10-01 00:30:50

标签: python django

我想向填写我的django应用中的电子邮件字段的用户发送邮件。这个过程是这样的,当用户填写表单时,如果它有效,它将保存在数据库中并从数据库中选择用户电子邮件地址并发送邮件。我尝试了下面的代码,但是我收到了这个错误:

   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

模型

class Invite(models.Model):
    email_address=models.EmailField(max_length=75)

    def __unicode__(self):
        return self.email_address

浏览

     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))

1 个答案:

答案 0 :(得分:1)

想一想Invite.objects.values('email_address')返回什么?绝对不是send_mail函数接受的内容(电子邮件地址列表)。

Invite.objects.values_list('email_address', flat=True)就是您所需要的。哦,然后删除[]

send_mail('foo', 'bar', 'baz@example.com', Invite.objects.values_list(...))