简体   繁体   中英

Error When Using Django send_mail() Function

I want to send a mail to users that fill the email field in my django app. 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? Definitely not what the send_mail function accepts (a list of email addresses).

Invite.objects.values_list('email_address', flat=True) is what you need. Oh, and remove the [] .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM