Django,如何为网站上显示的脚本制作下载按钮

时间:2015-12-13 19:22:46

标签: javascript python django download

在我的Django项目中,我显示了一个Python脚本,我想在它上面创建一个Download按钮,他们可以点击它并让他们的浏览器下载脚本。我怎样才能做到这一点?

使用Django 1.8.4。

1 个答案:

答案 0 :(得分:2)

这已在文档中记录 - https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

response = HttpResponse(my_data, content_type='text/x-python')
response['Content-Disposition'] = 'attachment; filename="foo.py"'