2016-12-14 58 views
0

我正在嘗試爲用戶下載動態構建KML文件。我正在使用python中的KML庫來生成和保存KML,但是我想以廣告下載的形式返回文件。本質上,如果我的應用程序中的用戶單擊鏈接bam,則用戶通過單擊鏈接生成並下載KML。我的代碼不工作,我猜我的回答是不正確設置:在視圖中創建文件並將其返回給Django

在views.py

def buildKML(request): 
    # Create the HttpResponse object with the appropriate PDF headers. 

    response = HttpResponse(content_type='application/kml') 
    response['Content-Disposition'] = 'attachment; filename="botanicalgarden.kml"' 
    #just testing the simplekml library for now 
    kml = simplekml.Kml() 
    kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)]) # lon, lat, optional height 
    kml.save('botanicalgarden.kml') 

    return response 

的錯誤我得到運行此方法時,我點擊鏈接或跳轉鏈接:

No results - Empty KML file

我想這是因爲文件名=,最終被保存不是在同一個。

回答

1

simplekml模塊也得到KML作爲字符串而不是保存爲文件的功能,所以首先從初始化字符串KML回報&響應的HttpResponse對象

kml = simplekml.Kml() 
kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)]) 
response = HttpResponse(kml.kml()) 
response['Content-Disposition'] = 'attachment; filename="botanicalgarden.kml"' 
response['Content-Type'] = 'application/kml' 
return response 
相關問題