django错误滚动列表以发送电子邮件用户

时间:2015-08-03 19:38:52

标签: python django

当用户在django中滚动浏览我的用户列表以向模板发送消息时,我有一个错误,您有任何其他表单或文档,agreadeceria 有人帮我,请

    # -*- coding: utf-8 -*-
    from django.shortcuts import render
    from django.views.generic import TemplateView
    from .models import Subscriber
    from django.core.mail import EmailMessage
    from django.core.mail import EmailMultiAlternatives
    from django.template import Context
    from django.template.loader import render_to_string
    from apps.users.models import User

html_content = render_to_string('mails/mailplantilla.html')
html_content = render_to_string('mails/email.txt'
email = EmailMultiAlternatives('hello')
usuarios = User.objects.all()
email.attach_alternative(html_content, "text/html")
email.frw = ['moises@cambio90.com']
    for u in usuarios:
        print(u.email)
        email.to = u.email
        email.send()

错误:这是我得到错误的视图q

> Internal Server Error: /favicon.ico Traceback (most recent call last):
> File
> "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/handlers/base.py",
> line 87, in get_response
>     response = middleware_method(request)   File "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/middleware/common.py",
> line 72, in process_request
>     if (not urlresolvers.is_valid_path(request.path_info, urlconf) and   File
> "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/urlresolvers.py",
> line 619, in is_valid_path
>     resolve(path, urlconf)   File "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/urlresolvers.py",
> line 494, in resolve
>     return get_resolver(urlconf).resolve(path)   File "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/urlresolvers.py",
> line 343, in resolve
>     for pattern in self.url_patterns:   File "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/urlresolvers.py",
> line 372, in url_patterns
>     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)   File
> "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/urlresolvers.py",
> line 366, in urlconf_module
>     self._urlconf_module = import_module(self.urlconf_name)   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
>     __import__(name)   File "/home/root-master/workspace/projectdjango/codigo/spoonrocket/spoonrocket/spoonrocket/urls.py",
> line 10, in <module>
>     url(r'^', include('apps.mails.urls')),   File "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/conf/urls/__init__.py",
> line 28, in include
>     urlconf_module = import_module(urlconf_module)   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
>     __import__(name)   File "/home/root-master/workspace/projectdjango/codigo/spoonrocket/spoonrocket/apps/mails/urls.py",
> line 3, in <module>
>     from views import IndexView   File "/home/root-master/workspace/projectdjango/codigo/spoonrocket/spoonrocket/apps/mails/views.py",
> line 44, in <module>
>     email.send()   File "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/mail/message.py",
> line 282, in send
>     if not self.recipients():   File "/home/root-master/workspace/projectdjango/env/menu/local/lib/python2.7/site-packages/django/core/mail/message.py",
> line 278, in recipients
>     return self.to + self.cc + self.bcc TypeError: coercing to Unicode: need string or buffer, list found

1 个答案:

答案 0 :(得分:0)

我认为最简单的方法就是格式化你的代码,请不要改变代码的格式和编辑方式,对我有用

html_content = render_to_string('mails/mailplantilla.html')
text_content = render_to_string('mails/email.txt')
usuarios = User.objects.all()
email.attach_alternative(html_content, "text/html")
from_email = 'moises@cambio90.com'
subject = 'My Subject'
for u in usuarios:
    to_email = ['%s' % u.email ]
    text_content = plaintext.render(d)
    html_content = htmly.render(d)
    msg = EmailMultiAlternatives(subject, text_content, from_email, to_email)
    msg.attach_alternative(html_content, "text/html")
    msg.send()

应该这样做