Python ';字节';对象没有属性';编码';在电子邮件中

Python ';字节';对象没有属性';编码';在电子邮件中,python,django,encoding,utf-8,attributeerror,Python,Django,Encoding,Utf 8,Attributeerror,我试图在Django中制作一个应用程序,它接受用户的信息,并通过电子邮件将HTML模板作为pdf发送给用户。但我得到了这个错误 'bytes'对象没有属性'encode' 这是我对电子邮件的看法 def email(request, serial_no): user = get_object_or_404(Student, pk=serial_no) # roll_no = {'roll': str(user['roll_no'])} html = render_to_s

我试图在Django中制作一个应用程序,它接受用户的信息,并通过电子邮件将HTML模板作为pdf发送给用户。但我得到了这个错误 'bytes'对象没有属性'encode'

这是我对电子邮件的看法

def email(request, serial_no):
    user = get_object_or_404(Student, pk=serial_no)
    # roll_no = {'roll': str(user['roll_no'])}
    html = render_to_string('card.html',
                            {'user': user})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename={}'.format(user.roll_no + '.pdf')
    pdf = weasyprint.HTML(string=html, base_url='').write_pdf(
        stylesheets=[weasyprint.CSS(string='body { font-family: serif}')])
    to_emails = [str(user.roll_no) + '@gmail.com']
    subject = "Certificate from Nami Montana"
    email = EmailMessage(subject, body=pdf, from_email='SSC', to=to_emails)
    email.attach("{}".format(user.roll_no) + '.pdf', pdf, "application/pdf")
    email.content_subtype = "pdf"  # Main content is now text/html
    email.encoding = 'utf-8'
    email.send()
    return HttpResponseRedirect(reverse('id_card:submit'))

这里是错误

Traceback (most recent call last):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Dell\Documents\GitHub\SSC-Website\id_card\views.py", line 49, in email
    email.send()
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\console.py", line 34, in send_messages
    self.write_message(message)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\console.py", line 17, in write_message
    msg = message.message()
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 246, in message
    msg = SafeMIMEText(self.body, self.content_subtype, encoding)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 159, in __init__
    MIMEText.__init__(self, _text, _subtype=_subtype, _charset=_charset)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\email\mime\text.py", line 42, in __init__
    self.set_payload(_text, _charset)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 167, in set_payload
    has_long_lines = any(
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 168, in <genexpr>
    len(line.encode()) > RFC5322_EMAIL_LINE_LENGTH_LIMIT
AttributeError: 'bytes' object has no attribute 'encode'

回溯(最近一次呼叫最后一次):
文件“C:\Users\Dell\AppData\Local\Programs\Python\38\lib\site packages\django\core\handlers\exception.py”,第47行,在内部
响应=获取响应(请求)
文件“C:\Users\Dell\AppData\Local\Programs\Python\38\lib\site packages\django\core\handlers\base.py”,第179行,在\u get\u响应中
响应=包装的回调(请求,*回调参数,**回调参数)
电子邮件中第49行的文件“C:\Users\Dell\Documents\GitHub\SSC Website\id\u card\views.py”
email.send()
文件“C:\Users\Dell\AppData\Local\Programs\Python38\lib\site packages\django\core\mail\message.py”,第284行,在send中
返回self.get\u连接(以静默方式失败)。发送消息([self])
文件“C:\Users\Dell\AppData\Local\Programs\Python\38\lib\site packages\django\core\mail\backends\console.py”,第34行,发送消息
自写消息(消息)
文件“C:\Users\Dell\AppData\Local\Programs\Python\38\lib\site packages\django\core\mail\backends\console.py”,第17行,在write\u消息中
msg=message.message()
文件“C:\Users\Dell\AppData\Local\Programs\Python38\lib\site packages\django\core\mail\message.py”,第246行,在消息中
msg=SafeMIMEText(self.body,self.content\u子类型,编码)
文件“C:\Users\Dell\AppData\Local\Programs\Python\38\lib\site packages\django\core\mail\message.py”,第159行,在uu init中__
MIMEText.\uuuu init\uuuuuuuuuuu(self,\u text,\u subtype=\u subtype,\u charset=\u charset)
文件“C:\Users\Dell\AppData\Local\Programs\Python38\lib\email\mime\text.py”,第42行,在\uuu init中__
self.set\u有效负载(\u文本,\u字符集)
文件“C:\Users\Dell\AppData\Local\Programs\Python\38\lib\site packages\django\core\mail\message.py”,第167行,在set\u有效负载中
有长的线吗(
文件“C:\Users\Dell\AppData\Local\Programs\Python38\lib\site packages\django\core\mail\message.py”,第168行,在
len(line.encode())>RFC5322\u电子邮件\u行\u长度\u限制
AttributeError:“字节”对象没有属性“encode”

提前感谢

问题是您将PDF文件对象作为邮件正文发送:

pdf=weasyprint.HTML(string=HTML,base\u url='')。编写pdf(
样式表=[weasyprint.CSS(string='body{font-family:serif}'))
email=EmailMessage(主题,正文=pdf,from_email='SSC',to=to_email)

EmailMessage
尝试使用utf-8编解码器对
正文
进行编码,但正文是一个PDF文件对象,即
字节
,并且
字节
不提供
编码
方法,而只提供
解码
方法。将PDF添加到电子邮件中的正确方法是在以后的cod中附加它e、 现在,如果您想让电子邮件正文成为PDF文件的文本,请在
str
中获取文本,并将其作为
body
kwarg传递。

请发布整个堆栈跟踪您编码
str
(Unicode字符串)。您解码
字节
(字节字符串).
是一个字节字符串,因此无法进行编码。无法识别代码中的大多数类型,因为它不完整。请创建一个。谢谢。您能帮我解决一个问题吗?当我试图用Adobe Reader打开pdf时,它显示“处理页面时出错。阅读此文档时出现问题(135)”@BibekPaul,可能有一些不受支持的字符(由Adobe Reader提供)在文件中。尝试用Chrome或。如果我给你看html,你能指出哪些字符是这些字符吗code@BibekPaul,不,我不知道所有不支持的utf-8字符,我只知道这在过去是个问题。我最好的建议是在支持字符的查看器中查看PDF,并怀疑特灵,稀有的。