【问题标题】:AttributeError: 'list' object has no attribute 'lstrip'AttributeError:“列表”对象没有属性“lstrip”
【发布时间】:2019-12-28 11:00:30
【问题描述】:

在过去的 3 个小时里,我试图向从数据库中获取的电子邮件列表发送一封带有 CSV 文件附件的电子邮件,但我收到了一个错误,我不确定它为什么会发生。 如果有任何 unicode 被通过等,我会仔细检查我的代码是否有错误,以避免我将获取的数据转换为 str 但没有用。

如果我独立运行脚本,它会毫无问题地执行并发送带有附件的电子邮件,但在 Django 中它会出错。

电子邮件列表用于检查错误是否与数据库相关,但不是。它也会显示在预定义的电子邮件列表中。

我从 Here

有一个Reference to such an error,但我认为它没有给我带来任何好处。

错误

正在执行系统检查...

Traceback 启动的线程中未处理的异常(最近一次调用最后一次):文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\autoreload.py", 第 228 行,在包装器中 fn(*args, **kwargs) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\management\commands\runserver.py", 第 124 行,inner_run self.check(display_num_errors=True) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\management\base.py", 第 359 行,检查中 include_deployment_checks=include_deployment_checks,文件“C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\management\base.py”, 第 346 行,在 _run_checks 返回 checks.run_checks(**kwargs) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\registry.py", 第 81 行,在 run_checks new_errors = check(app_configs=app_configs) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\urls.py", 第 16 行,在 check_url_config 中 return check_resolver(resolver) 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\urls.py", 第 26 行,在 check_resolver 中 返回 check_method() 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\urls\resolvers.py", 第 256 行,检查中 对于 self.url_patterns 中的模式:文件“C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\functional.py”, 第 35 行,在 get 中 res = instance.dict[self.name] = self.func(instance) File "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django \urls\resolvers.py", 第 407 行,在 url_patterns 中 模式= getattr(self.urlconf_module,“urlpatterns”,self.urlconf_module)文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\functional.py", 第 35 行,在 get 中 res = instance.dict[self.name] = self.func(instance) File "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django \urls\resolvers.py", 第 400 行,在 urlconf_module 中 返回 import_module(self.urlconf_name) 文件“c:\python27\Lib\importlib__init__.py”,第 37 行,在 import_module 导入(名称)文件“C:\Users\Bitswits 3\Desktop\LCRProject\LeastCostRouting\LeastCostRouting\urls.py”,行 12、在 url('', include('LCR.urls')), 文件 "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\conf\urls__init__.py", 第 50 行,包括 urlconf_module = import_module(urlconf_module) 文件“c:\python27\Lib\importlib__init__.py”,第 37 行,在 import_module import(名称)文件“C:\Users\Bitswits 3\Desktop\LCRProject\LeastCostRouting\LCR\urls.py”,第 25 行,在 url(r'^(?i)someemail/$', views.lcr_send_email(), name='EE-Page'), 文件 "C:\Users\Bitswits 3\Desktop\LCRProject\LeastCostRouting\LCR\views.py",第 422 行,在 lcr_send_email server.sendmail(from_address, email_list, msg.as_string()) 文件“c:\python27\Lib\email\message.py”,第 137 行,在 as_string g.flatten(self, unixfrom=unixfrom) 文件“c:\python27\Lib\email\generator.py”,第 83 行,扁平化 self._write(msg) 文件“c:\python27\Lib\email\generator.py”,第 115 行,在 _write self._write_headers(msg) 文件“c:\python27\Lib\email\generator.py”,第 164 行,在 _write_headers v, maxlinelen=self._maxheaderlen, header_name=h).encode() 文件“c:\python27\Lib\email\header.py”,第 410 行,在编码中 value = self._encode_chunks(newchunks, maxlinelen) 文件“c:\python27\Lib\email\header.py”,第 370 行,在 _encode_chunks _max_append(chunks, s, maxlinelen, extra) 文件“c:\python27\Lib\email\quoprimime.py”,第 97 行,在 _max_append L.append(s.lstrip()) AttributeError: 'list' object has no attribute 'lstrip'

代码

def lcr_send_email():


    template = "LCR\LCR_Email.html"

    email_list = []
    vendor_list = []

    email_data = RecipientEmails.objects.values_list(
        'recipient_email', flat=True)

    for i in email_data:
        email_list.append(str(i))


    for z in email_list:
        print type(z)

    vendor_name = Vendor.objects.values_list('ven_name', flat=True)
    for i in vendor_name:
        vendor_list.append(str(i))

    emails =[
'foo@example.com',
'bar@example.com',
...
]

    subject = vendor_list
    password = settings.EMAIL_HOST_PASSWORD
    email = settings.EMAIL_HOST_USER  # email from
    smtp_server = "smtp.gmail.com"
    smtp_port = 587
    from_address = settings.EMAIL_HOST_USER
    from_password = settings.EMAIL_HOST_PASSWORD
    mail_body = "This is an automated respose"
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['To'] = email_list

    msg.attach(MIMEText(mail_body))

    file = "MyCsv.csv"
    part = MIMEBase('application', "octet-stream")
    part.set_payload(open(file, "rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition','attachment; filename="{0}"'.format(os.path.basename(file)))
    msg.attach(part)

    server = smtplib.SMTP(smtp_server, smtp_port)
    server.starttls()
    server.login(from_address, from_password)
    server.sendmail(from_address, email_list, msg.as_string())
    server.quit()

【问题讨论】:

  • 哪个版本的 django?
  • Django = 1.11.17 Python = 2.7
  • 这里发生了很多奇怪的事情。为什么不使用 Django 提供的邮件功能,甚至不使用 smtplib send_email 消息,而不是真正低级的 sendmail?但要解决您的问题,我们可能需要查看 EMAI_HOST_USER 值。
  • @DanielRoseman 是 DjangoEmail@gmail.com

标签: python django email smtp


【解决方案1】:

虽然 Python 通过 smtplib 模块发送电子邮件相对容易,但 Django 提供了几个轻量级的包装器。提供这些包装器是为了让发送电子邮件更加快速,在开发过程中轻松测试电子邮件发送,并为无法使用 SMTP 的平台提供支持。

https://docs.djangoproject.com/en/1.11/topics/email/

【讨论】:

  • 不完全是我正在寻找的答案,但它有效。你能解释一下为什么我的代码没有运行吗?
  • @Djang000 我认为这并不重要。我们将 Django 升级到 2.2,将 Python 升级到 3.7,以便在我们公司进行生产。所以我建议你尝试一下。
猜你喜欢
  • 2016-09-05
  • 2021-07-05
  • 2018-01-16
  • 2016-05-14
  • 2016-12-21
  • 2022-01-23
  • 2022-01-23
  • 2019-11-01
  • 2021-08-23
相关资源
最近更新 更多