首页 > 解决方案 > AttributeError:“列表”对象没有属性“lstrip”

问题描述

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

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

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

我从 这里得到了片段

对此类错误的参考, 但我认为它没有给我带来任何好处。

错误

正在执行系统检查...

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 返回 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 行,在在 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) File "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\urls.py",第 26 行,在 check_resolver return 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 行,在在 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) File "C:\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\core\checks\urls.py",第 26 行,在 check_resolver return 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 行,在在 check_url_config 返回 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 行,在在 check_url_config 返回 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 行,在\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\functional.py",第 35 行,在\Users\BITSWI~1\Desktop\LCRPRO~1\VE\lib\site-packages\django\utils\functional.py",第 35 行,在获取 res = 实例。dict [self.name] = self.func(instance) 文件“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) 文件“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 导入中(名称)文件“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 行,编码值 = self._encode_chunks(newchunks, maxlinelen) 文件“c:\python27\Lib\email\header.py”,第 370 行,在 _encode_chunks 中' 对象没有属性 'lstrip'lstrip'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()

标签: pythondjangoemailsmtp

解决方案


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

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


推荐阅读