在vi中创建一个文件并将其返回到Django

2024-07-08 10:47:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个KML文件的动态用户下载。我正在使用python中的一个KML库来生成和保存KML,但是我想以adownload的形式返回该文件。本质上,如果我的应用程序中的用户单击某个链接bam,则KML将由单击该链接的用户生成和下载。我的代码不起作用,我猜我的响应设置不正确:

在视图.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

我想这是因为文件名=和保存的final不是一回事。在


Tags: 文件the用户应用程序链接response动态kml
1条回答
网友
1楼 · 发布于 2024-07-08 10:47:56

对于simplekml模块,有一个函数将kml作为字符串而不是另存为文件,因此首先从kml string初始化响应并返回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

相关问题 更多 >

    热门问题