The excel file generated cannot be open when my project started with uwsgi
as it can be opened successfully with manage.py runserver
My main codes like below:
── test_excel├── urls.py├── wsgi.py└── settings.py── manage.py
urls.py
from django.contrib import adminfrom django.urls import pathfrom django.views import Viewfrom openpyxl.writer.excel import save_virtual_workbookimport pandas as pdfrom django.http import HttpResponse, StreamingHttpResponseimport xlrdimport openpyxlfrom openpyxl import Workbookfrom openpyxl.styles import Font, Alignmentfrom openpyxl.writer.excel import save_virtual_workbookfrom openpyxl.cell.cell import ILLEGAL_CHARACTERS_REclass TestExcelView(View): def get(self, request): # indexs = {0: ['cost', '2020-01', 'testing'], # 1: ['cost', '2020-01', '360 Limited'], # 2: ['cost', '2020-02', 'Korea Co.,LTD'], # 3: ['cost', '2020-02', 'ADS4EACH HK TECH LIMITED']} # columns = [['1'], ['amy'], ['tom'], ['zara'], ['jay'], ['Lee'], ['Uzi'], ['Zome'], ['Qoe'], ['Aoi'], ['Yeezy'], # ['Hazy'], ['Wash'], ['pany'], ['zoey'], ['Moe'], ['total']] # datas = { # 0: [0.0, 0.0, 0.0, 0.0, 0.0, 7.85, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.85], # 1: [0.0, 0.0, 0.0, 0.0, 0.0, 7.85, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.85], # 2: [0.0, 0.0, 0.0, 0.0, 0.0, 7.85, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.85], # 3: [0.0, 0.0, 0.0, 0.0, 0.0, 7.85, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.85] # } # # index = pd.MultiIndex.from_tuples(indexs.values()) # column = pd.MultiIndex.from_tuples(columns) # data = datas.values() # # df = pd.DataFrame(data, index=index, columns=column) # # # 1 saved as excel # # excel_writer = pd.ExcelWriter(path='download.xlsx', engine='openpyxl') # df.to_excel(excel_writer) # wb = excel_writer.book # # response = HttpResponse(save_virtual_workbook(wb)) # response["Content-Type"] = 'application/vnd.ms-excel' # response['Content-Disposition'] = 'attachment; filename={}.xlsx'.format("for_test") # return response wb = Workbook() ws = wb.active ws.merge_cells('A1:B1') a1 = ws["A1"] ws["A1"] = "reason" ws.column_dimensions["A"].width = 100 ali = Alignment(horizontal='center', vertical='center') a1.alignment = ali ws["A2"] = "request_id" ws.column_dimensions["A"].width = 50 ws["B2"] = "rebate_reason" ws.column_dimensions["B"].width = 50 for item in ws["A2:B2"][0]: item.font = Font(color='FF0000') item.alignment = ali response = HttpResponse(save_virtual_workbook(wb)) response["Content-Type"] = 'application/vnd.ms-excel' response['Content-Disposition'] = 'attachment; filename={}.xlsx'.format("test_excel") return responseurlpatterns = [ path('admin/', admin.site.urls), path('test/', TestExcelView.as_view()),]
wsgi.py
import osfrom django.core.wsgi import get_wsgi_application#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_excel.settings')application = get_wsgi_application()# test_excel.ini[uwsgi]pythonpath=/home/bluev/work/bv_crm_bak/test_excelpythonpath=/home/bluev/work/bv_crm_bak/test_excelenv=DJANGO_SETTINGS_MODULE=test_excel.settingsmodule=test_excel.wsgimaster=Truepidfile=logs/test_excel.pidvacuum=Truemax-requests=100000enable-threads=trueprocesses = 4threads=8listen=64daemonize=logs/test_excel.loglog-slow=3000python-autoreload=1http=0.0.0.0:8876
Then if i start the project like uwsgi --ini test_excel.ini
, and i request localhost:8876/test/
, i would get a failed excel file. But if i start it as python manage.py reunserver
the excel can be opened as expected.
How can I get the right excel file response with uwsgi
in this situation?
Thansks.