如何在Django HTTP响应中正确发送带有其他变量的文件?

问题描述 投票:0回答:1

我知道如何返回JSON响应:

return Response({'example':'example'})

并且还知道如何返回文件响应:

response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="mypdf.pdf"'
return response

但是如果我想在一个响应中同时发送两个,我该如何[[正确这样做? Django documentation给出了设置其他标题字段的示例:

>>> response = HttpResponse() >>> response['Age'] = 120
但是从我看到的一对answers来看,看起来好像是在标头中发送业务数据,这不是走的路。有更好的解决方案吗? 
django response httpresponse
1个回答
0
投票
通常使用Content-Disposition,有两个选项:

    将其他数据放入标题中。
  • 将元信息存储在值中,例如attachment; key=value; filename="my.txt"
© www.soinside.com 2019 - 2024. All rights reserved.