Django csv HttpResponse在当地工作破产

时间:2017-04-10 09:58:27

标签: python django csv azure

我有一个django视图,允许用户根据日期选择导出csv。本地此功能正常。

我在Azure上托管了这个网络应用程序。

在Azure中,我在csv响应URL上收到404(未找到)错误。 我的猜测是我需要在settings.py中更改生产内容或在Azure中更改以允许csv下载。

观看代码:

    def report(request):
       if request.method == "POST":
           ...
         do some stuff
           ...

       else:          
            if 'emp-csv' in request.GET:
            # data from date selection
            data = request.GET['data']
            # Convert string to list
            cleandata = ast.literal_eval(data)
            # creating csv response
            response = HttpResponse(content_type="text/csv")
            response['Content-Disposition'] = "attachment; filename='hours_report.csv'"
            # Write data to csv file and return
            fieldnames = ['Supervisor', 'EmployeeID', 'FirstName', 'LastName', 'NT', 'OT', 'OT2', 'Project', 'Client', 'date']
            writer = csv.DictWriter(response, fieldnames=fieldnames, dialect='excel', )
            writer.writeheader()
            writer.writerows(cleandata)

            return response

网址代码:

url(r'employees/report/export', hours_views.report, name="export_csv"),

Azure中生成的网址:

https://xyzxyz.azurewebsites.net/employees/report/export?csrfmiddlewaretoken=(..CSRF_TOKEN)&data=(...DATA)

的web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <handlers>
   <add name="httpPlatformHandler" path="*" verb="*"
         modules="httpPlatformHandler" resourceType="Unspecified" />
  </handlers>

  <httpPlatform processPath="D:\home\Python27\python.exe"
 arguments="run_waitress_server.py" requestTimeout="00:04:00" 
  startupTimeLimit="120" startupRetryCount="3" stdoutLogEnabled="true">
   <environmentVariables>
    <environmentVariable name="PYTHONPATH" value="D:\home\site\wwwroot" />
    <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
   </environmentVariables>
  </httpPlatform>
 </system.webServer>
</configuration>

Waitress.py文件:

import os
from waitress import serve
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "isi_django.settings")
application = get_wsgi_application()
serve(application, host="0.0.0.0", port=os.environ["PORT"], url_scheme='https')

1 个答案:

答案 0 :(得分:0)

解决了这个问题,感谢Django / django gitter频道的一位用户的帮助(向@MBijman大喊)。

在URL中传递csv导出的数据导致了问题。 我更改了视图代码以将cookie值设置为数据:

request.session['emp-data'] = emp_data

然后我将csv导出分隔为一个新视图:

@login_required
def csv_report(request):
# If request is for Excel report
if request.method == 'GET':
    if 'emp-csv' in request.GET:
        # data from date selection
        data = request.session['emp-data']
        # creating csv response
        response = HttpResponse(content_type="text/csv")
        response['Content-Disposition'] = "attachment; filename='hours_report.csv'"
        # Write data to csv file and return
        fieldnames = ['Supervisor', 'EmployeeID', 'FirstName', 'LastName', 'NT', 'OT', 'OT2', 'Project', 'Client',
                  'date']
        writer = csv.DictWriter(response, fieldnames=fieldnames, dialect='excel', )
        writer.writeheader()
        writer.writerows(data)

        return response