The front-end submits query conditions, downloads Excel data files that meet the conditions, and uses the xlwt library in the back-end view to return the results, for example:
Objs = units. Objects. All () # when creating a workbook, if you need to write Chinese characters, use UTF-8 encoding. The default value is Unicode encoding. WB = xlwt. workbook (encoding = 'utf-8') Ws = WB. add_sheet ('accessory price') WS. write (0, 0, 'accessory number') WS. write (0, 1, 'accessory name') WS. write (0, 2, 'accessory price') WS. write (0, 3, 'mall ') excel_row = 1for OBJ in objs: Ws. write (excel_row, 0, obj. unitid) WS. write (excel_row, 1, obj. unitname) WS. write (excel_row, 2, obj. price) WS. write (excel_row, 3, obj. marketid) excel_row = excel_row + 1 # ------ start: This code can be replaced by the following comment segment, which should be saved as a file format and changed to save as a data stream, in order to return to the front-end download response = httpresponse (content_type = 'application/vnd. MS-Excel ') response ['content-disposition'] = 'attachment; filename=example.xls 'wb. save (response) return response # ------ end "SiO = stringio. stringio () WB. save (SIO) response = httpresponse (SIO. getvalue (), content_type = 'application/vnd. MS-Excel ') response ['content-disposition'] = 'attachment; filename=test.xls 'Return response """
Django dynamic File Download