Python Django-ical依恋原因';HttpResponse';对象没有属性';编码';

Python Django-ical依恋原因';HttpResponse';对象没有属性';编码';,python,django,icalendar,Python,Django,Icalendar,当我将ics日历邀请附加到Django中发送的电子邮件时,我得到了AttributeError:“HttpResponse”对象没有属性“encode”。这只是如果我附加了ical邀请,如果我不附加,一切都很好 我不知道它为什么要在HttpResponse对象上查找encode属性,或者如何处理它 def email_invite(ics_form, user_assigned): subject = 'You Have Been Assigned Schuduled Maintance

当我将ics日历邀请附加到Django中发送的电子邮件时,我得到了AttributeError:“HttpResponse”对象没有属性“encode”。这只是如果我附加了ical邀请,如果我不附加,一切都很好

我不知道它为什么要在HttpResponse对象上查找encode属性,或者如何处理它

def email_invite(ics_form, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(ics_form['Filename'], ics_form, 'text/calendar')
    email.send()


def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    response = HttpResponse(cal.serialize(), content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

 File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 280, in _create_message
    return self._create_attachments(msg)
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 293, in _create_attachments
    msg.attach(self._create_attachment(*attachment))
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 320, in _create_attachment
    attachment = self._create_mime_attachment(content, mimetype)
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in _create_mime_attachment
    attachment = SafeMIMEText(content, subtype, encoding)
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 126, in __init__
    MIMEText.__init__(self, text, subtype, charset)
  File "/usr/lib/python2.7/email/mime/text.py", line 30, in __init__
    self.set_payload(_text, _charset)
  File "/usr/lib/python2.7/email/message.py", line 226, in set_payload
    self.set_charset(charset)
  File "/usr/lib/python2.7/email/message.py", line 268, in set_charset
    cte(self)
  File "/usr/lib/python2.7/email/encoders.py", line 73, in encode_7or8bit
    orig.encode('ascii')
AttributeError: 'HttpResponse' object has no attribute 'encode'
[15/Jan/2014 08:30:36] "POST /modify/2/ HTTP/1.1" 500 167107
Not Found: /favicon.ico

您尚未粘贴所有代码,但听起来您正在执行以下操作:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)
这会引发您看到的异常,因为
ics\u form
是一个
HttpResponse
对象。。。
attach
方法希望文件的内容作为字符串附加

也许您应该让
create_ics
函数返回ical内容,并在单独的视图函数中返回HttpResponse。比如:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)

您尚未粘贴所有代码,但听起来您正在执行以下操作:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)
这会引发您看到的异常,因为
ics\u form
是一个
HttpResponse
对象。。。
attach
方法希望文件的内容作为字符串附加

也许您应该让
create_ics
函数返回ical内容,并在单独的视图函数中返回HttpResponse。比如:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)

您尚未粘贴所有代码,但听起来您正在执行以下操作:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)
这会引发您看到的异常,因为
ics\u form
是一个
HttpResponse
对象。。。
attach
方法希望文件的内容作为字符串附加

也许您应该让
create_ics
函数返回ical内容,并在单独的视图函数中返回HttpResponse。比如:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)

您尚未粘贴所有代码,但听起来您正在执行以下操作:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)
这会引发您看到的异常,因为
ics\u form
是一个
HttpResponse
对象。。。
attach
方法希望文件的内容作为字符串附加

也许您应该让
create_ics
函数返回ical内容,并在单独的视图函数中返回HttpResponse。比如:

ical = create_ics(thedata)
email_invite(ical, theuser)
def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)

Django EmailMessage()是否将所有附件计算为HttpResponse对象?如何使ics_表单不是HttpResponse对象?请尝试阅读并理解代码。。。按照您的方式,
create_ics
函数返回一个
HttpResponse
对象。Django
EmailMessage
不需要HttpResponse。。。这就是你所面临的问题我在上面发布的代码是你可能需要的粗略版本。。。我重写了
create_ics
函数,以不返回
HttpResponse
Django EmailMessage()是否将所有附件都计算为HttpResponse对象?如何使ics_表单不是HttpResponse对象?请尝试阅读并理解代码。。。按照您的方式,
create_ics
函数返回一个
HttpResponse
对象。Django
EmailMessage
不需要HttpResponse。。。这就是你所面临的问题我在上面发布的代码是你可能需要的粗略版本。。。我重写了
create_ics
函数,以不返回
HttpResponse
Django EmailMessage()是否将所有附件都计算为HttpResponse对象?如何使ics_表单不是HttpResponse对象?请尝试阅读并理解代码。。。按照您的方式,
create_ics
函数返回一个
HttpResponse
对象。Django
EmailMessage
不需要HttpResponse。。。这就是你所面临的问题我在上面发布的代码是你可能需要的粗略版本。。。我重写了
create_ics
函数,以不返回
HttpResponse
Django EmailMessage()是否将所有附件都计算为HttpResponse对象?如何使ics_表单不是HttpResponse对象?请尝试阅读并理解代码。。。按照您的方式,
create_ics
函数返回一个
HttpResponse
对象。Django
EmailMessage
不需要HttpResponse。。。这就是你所面临的问题我在上面发布的代码是你可能需要的粗略版本。。。我已重写了
create_ics
函数,以不返回
HttpResponse