由于对象属性不存在而发生错误,因为它不存在

时间:2018-09-07 23:12:26

标签: python django sendgrid-api-v3

我正在尝试使用SendGrid python API发送电子邮件。为此,我使用以下功能:

def send_contact_us_email(sender=None, name=None, subject=None, message=None):
status_code = None
if sender and name and subject and message:
    to = "test@test.com"
    email = Email.objects.create(to=to, sender=sender, subject=subject, content=message)
    status_code = email.send()

return status_code

因此,我使用电子邮件模型存储电子邮件以供以后参考,以便将发送电子邮件的代码放入模型中。这是电子邮件模型:

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *

sg = SendGridAPIClient(apikey=getattr(settings, 'SENDGRID_API_KEY'))

class Email(models.Model):
    to = models.EmailField(max_length=100)
    sender = models.EmailField(max_length=100)
    subject = models.CharField(max_length=100)
    content = models.TextField()

    def __str__(self):
        return 'to: {to} from: {sender}'.format(to=self.to, sender=self.sender)

    def send(self):
        if self.to and self.sender and self.subject and self.content:
            to_email = Email(self.to)
            from_email = Email(self.sender)
            content = Content("text/html", self.content)
            mail = Mail(from_email, self.subject, to_email, content)
            response = sg.client.mail.send.post(request_body=mail.get())
            return response.status_code
        return 600 # incomplete information, cannot send email.

运行此代码时,我希望它能正常工作;我创建了一个包含所有必需数据的电子邮件模型对象,以发送电子邮件,然后使用sendgrid帮助器类构造一个JSON请求主体,以供SendGrid API处理。

但是,出现以下错误:

AttributeError at /contact-us/
'Email' object has no attribute 'get'
Request Method: POST
Request URL:    http://127.0.0.1:8000/contact-us/
Django Version: 1.11.13
Exception Type: AttributeError
Exception Value:    
'Email' object has no attribute 'get'
Exception Location: /Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/sendgrid/helpers/mail/personalization.py in add_to, line 39
Python Executable:  /Users/tomfinet/Desktop/royaleaccounts/env/bin/python
Python Version: 2.7.15
Python Path:    
['/Users/tomfinet/Desktop/royaleaccounts/env/source',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python27.zip',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/plat-darwin',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/plat-mac',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/lib-tk',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/lib-old',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages']
Server time:    Fri, 7 Sep 2018 23:04:16 +0000
Traceback Switch to copy-and-paste view
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/django/core/handlers/exception.py in inner
            response = get_response(request) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/source/royaleaccounts/views.py in contact_view
        status_code = send_contact_us_email(sender, name, subject, message) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/source/emails/utils.py in send_contact_us_email
        status_code = email.send() ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/source/emails/models.py in send
            mail = Mail(from_email, self.subject, to_email, content) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/sendgrid/helpers/mail/mail.py in __init__
            personalization.add_to(to_email) ...
▶ Local vars
/Users/tomfinet/Desktop/royaleaccounts/env/lib/python2.7/site-packages/sendgrid/helpers/mail/personalization.py in add_to
        self._tos.append(email.get()) ...
▶ Local vars
  

电子邮件对象没有属性get

但是,当我查看sendgrid helper类时,电子邮件类确实具有get属性:

def get(self):
    """
    Get a JSON-ready representation of this Email.

    :returns: This Email, ready for use in a request body.
    :rtype: dict
    """
    email = {}
    if self.name is not None:
        email["name"] = self.name

    if self.email is not None:
        email["email"] = self.email
    return email

因此,我无法解决此错误,我想念什么?

1 个答案:

答案 0 :(得分:1)

当我编写Email(self.sender)之类的东西时发现,它使用的是我的模型,而不是sendgrid编写的helper Email类。因此,只需将sendgrid中的Email导入为Emaill或其他内容即可。

from sendgrid.helpers.mail import Email as Emaill