Django dynamic File Download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.