简体   繁体   English

从 django 上的网站下载文件

[英]Downloading files from a website on django

I have a website on a debian server.我在 debian 服务器上有一个网站。 I fill out the template in docx format and save it.我用docx格式填写模板并保存。 How do I download it to the user's devices?如何将其下载到用户的设备?

 post_data = json.loads(request.body)
    date = post_data.get('date', False)
    price = post_data.get('price', False)
    pricesum = post_data.get('pricesum', False)
    startdate = post_data.get('startdate', False)
    enddate = post_data.get('enddate', False)


    doc = DocxTemplate('template.docx')

    dates = date
    prices = price

    tbl_contents = [{'expirationdate': expirationdate, 'price': price}
                    for expirationdate, price in zip(dates, prices)]

    context = {
        'tbl_contents': tbl_contents,
        'finalprice': sum(prices),
        'startdate': startdate,
        'enddate': enddate
    }

    doc.render(context)
    doc.save("static.docx")

I faced the same issue and I fixed it with this method.我遇到了同样的问题,我用这种方法修复了它。
My answer is inspired by this thread I hope it helps.我的回答受到了这个帖子的启发,希望对您有所帮助。

def fun(request):
    ...
    response = HttpResponse(content_type='application/vnd.openxmlformats- 
    officedocument.wordprocessingml.document')
    response['Content-Disposition'] = 'attachment; filename=file.docx'
    doc.save(response)
    return response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM