我正在使用以下方法将 pandas 数据框下载为 csv 文件:
视图.py ....
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="' + filename + '"'
my_df.to_csv(response, encoding='utf-8', index=False, )
return response
Run Code Online (Sandbox Code Playgroud)
在我的 html 中,我有...
<form class="default_form" method="POST" action="do_export">
{% csrf_token %}
<br/>
<input type="submit" class="btn btn-primary exportOut" value="Do Export"
id="myExport"/>
<br/>
</form>
Run Code Online (Sandbox Code Playgroud)
文件导出工作正常。但是,导出完成后,我需要能够在 html 页面上显示一条消息...类似 {{ my_message}} 的内容,而无需重新加载页面。
如果这是可行的,我将不胜感激关于如何做到这一点的任何建议,而无需诉诸 AJAX,我放弃了 AJAX,因为某些下载可能很大。
我有一个将放置在 Docker 容器中的 Django 应用程序。
该应用程序以 Dataframe 格式准备数据。我想允许用户将数据作为 excel 文件下载到他/她的本地驱动器。
我过去曾使用过 df.to_excel,但这在这种情况下不起作用。
请建议最好的方法来做到这一点。
我有一个基于函数的 Django API,如本示例所示:
@api_view(['GET'])
def findData(request):
dataId = request.GET['dataId']
page_query_param = 'page'
page_size = request.GET['page_size']
paginator = PageNumberPagination()
paginator.page_size = page_size
paginator.page_query_param = page_query_param
qry_set = Data.objects.all()
serializer = dataIdSerializer(qry_set, many=True)
theData= serializer.data
return Response(paginator.get_paginated_response(theData))
Run Code Online (Sandbox Code Playgroud)
但我不断收到此错误:
AttributeError: 'PageNumberPagination' object has no attribute 'page'
Run Code Online (Sandbox Code Playgroud)
我尝试设置
paginator.page_query_param = page_query_param
Run Code Online (Sandbox Code Playgroud)
但仍然是同样的问题。请告知如何修复。
我将新行附加到现有的熊猫数据框,如下所示:
df= df.append(pd.Series(), ignore_index=True)
Run Code Online (Sandbox Code Playgroud)
这导致主题 DeprecationWarning。
现有的 df 混合了 string、float 和 dateime.date 数据类型(总共 8 列)。
有没有办法在 df.append 中显式指定列类型?