1

我是通过 django_ical 提供一个 icalender 文件的服务器。问题是文件名为download.ics。我正在尝试将其更改为 MyCalender.ics。如果找到这个旧片段。我更喜欢使用 django_ical,因为它可以很好地与 django 联合。

cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH'  # IE/Outlook needs this
for event in event_list:
     vevent = cal.add('vevent')
icalstream = cal.serialize()
response = HttpResponse(icalstream, mimetype='text/calendar')
response['Filename'] = 'filename.ics'  # IE needs this
response['Content-Disposition'] = 'attachment; filename=filename.ics'
4

1 回答 1

3

django_ical继承ICalFeed自_django.contrib.syndication.views.Feed

在您的应用程序中,您继承自ICalFeedto provideitems以及item_title为 ics 文件生成数据的其他方法。

您可以覆盖该__call__方法。调用super将返回您HttpResponse,您将为其添加自定义标头。

代码将类似于:

class EventFeed(ICalFeed):
    """
    A simple event calender
    """
    product_id = '-//example.com//Example//EN'
    timezone = 'UTC'

    def items(self):
        return Event.objects.all().order_by('-start_datetime')

    # your other fields

    def __call__(self, request, *args, **kwargs):
        response = super(EventFeed, self).__call__(request, *args, **kwargs)
        if response.mimetype == 'text/calendar':
            response['Filename'] = 'filename.ics'  # IE needs this
            response['Content-Disposition'] = 'attachment; filename=filename.ics'
        return response

此代码未经测试,因此可能存在一些拼写错误。此外,如果调用有错误,您还需要捕获super. 我这样做了,response.mimetype == 'text/calendar'但也许有更好的方法来做到这一点

于 2012-11-01T16:33:28.130 回答