2012-11-01 17 views
1

我是通過django_ical服務器的icalender文件。問題是該文件被命名爲download.ics。我試圖將其更改爲MyCalender.ics。如果發現這個舊片段。我更喜歡使用django_ical,因爲它與django syndication很好地融合在一起。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' 
+0

你可以發佈你的django_ical代碼嗎? – dm03514

回答

3

django_icalICalFeeddjango.contrib.syndication.views.Feed

在您的應用程序,你從ICalFeed繼承繼承提供itemsitem_title等方法生成的集成電路芯片的文件數據。

您可以覆蓋__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',但也許有更好的辦法做到這一點

+0

我有同樣的解決方案。謝謝。 – RParadox