【问题标题】:Error When Using Django send_mail() Function使用 Django send_mail() 函数时出错
【发布时间】:2012-10-01 08:51:38
【问题描述】:

我想向在我的 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))

【问题讨论】:

    标签: python django


    【解决方案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(...))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-08
      • 2012-03-14
      • 1970-01-01
      • 2016-01-03
      • 2015-08-10
      • 2016-04-01
      • 2011-10-11
      • 1970-01-01
      相关资源
      最近更新 更多