簡體   English   中英

使用Django send_mail()函數時出錯

[英]Error When Using Django send_mail() Function

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

考慮一下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(...))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM