无法在Django 2.2中下载产品图片产品文件

时间:2019-06-14 15:08:37

标签: javascript python html django

您好,我正在使用django 2.2,我创建了产品下载功能,但是仍然无法下载产品,只是单击下载链接时才进行预览。

views.py

def download_product(request,filename):
        filepath = os.path.join(settings.MEDIA_ROOT,filename)
        guessed_type = guess_type(filepath)[0]
        wrapper = FileWrapper(open(filepath,'rb'))
        mimetype = 'application/force-download'
        if guessed_type:
            mimetype = guessed_type
        response = HttpResponse(wrapper, content_type=mimetype)
        response["Content-Disposition"] = "attachment; filename=%s" % smart_str(filename)

        response["X-SendFile"] = filepath
        return response

urls.py

urlpatterns = [
    url(r'^', product_list, name='product-list'),
    url(r'^(?P<slug>.*)/download/(?P<filename>.*)$',download_product,name="download_product")
]

在我的

内部

模板

{% if object.download %}
            <p><a href='{{ object.download.url }}'>Download</a></p>
            {% endif %}

0 个答案:

没有答案