您好,我正在使用django 2.2,我创建了产品下载功能,但是仍然无法下载产品,只是单击下载链接时才进行预览。
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 %}