Django 电子邮件出现错误“'list' 对象没有属性 'splitlines'”

Django email is giving error ''list' object has no attribute 'splitlines''

我认为这是我的电子邮件发送代码:

def send_email():
    if dasa == 1:
        send_mail(
            'Test available',
            available_tests,
            'from_email',
            ['to_email'],
            fail_silently=False,
        )

当我在网站上 运行 时,我得到了这个错误:

AttributeError at /tests/
'list' object has no attribute 'splitlines'
Request Method: GET
Request URL:    https://myapp.herokuapp.com/tests/
Django Version: 4.0.1
Exception Type: AttributeError
Exception Value:    
'list' object has no attribute 'splitlines'
Exception Location: /app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 169, in set_payload
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.9.10
Python Path:    
['/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python39.zip',
 '/app/.heroku/python/lib/python3.9',
 '/app/.heroku/python/lib/python3.9/lib-dynload',
 '/app/.heroku/python/lib/python3.9/site-packages']
Server time:    Fri, 18 Feb 2022 03:06:30 +0000
Traceback Switch to copy-and-paste view
/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/exception.py, line 47, in inner
                response = get_response(request) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/base.py, line 181, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
Local vars
/app/Tests/views.py, line 35, in check
        send_mail('Test available', available_tests, 'from_email@gmail.com', ['to_email@gmail.com'], fail_silently=False) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/__init__.py, line 61, in send_mail
    return mail.send() …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 284, in send
        return self.get_connection(fail_silently).send_messages([self]) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/backends/smtp.py, line 109, in send_messages
                sent = self._send(message) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/backends/smtp.py, line 123, in _send
        message = email_message.message() …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 246, in message
        msg = SafeMIMEText(self.body, self.content_subtype, encoding) …
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 159, in __init__
        MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset) …
Local vars
/app/.heroku/python/lib/python3.9/email/mime/text.py, line 42, in __init__
Local vars
/app/.heroku/python/lib/python3.9/site-packages/django/core/mail/message.py, line 169, in set_payload
                for line in payload.splitlines()

我该如何解决?我直接从 django 教程中获取了说明,所以我不知道为什么它不起作用。我也找不到任何关于这个 'splitlines' 属性的信息。

Django 的 send_email 需要一个字符串作为消息参数。所以你需要申请例如join 首先在 available_tests 列表变量上。根据变量的结构,一个简单的 "\n".join(available_tests) 就足够了。或者 <br> 如果您有 HTML 消息。