[Answered ]-Downloading a file from Django template on click doesn't work

1👍

You should use FileResponse from django.http from django.http import FileResponse.

    return FileResponse(open(model_file_path, 'rb'), as_attachment=True)
else:
    return FileResponse(open(model_file_path, 'rb'), as_attachment=True)

Edit:

def success(request):
    model_file_path = request.session.get('model_file_path')
    if request.method == 'POST':
        return render(request, 'success.html')
    else:
        return FileResponse(open(model_file_path, 'rb'), as_attachment=True)

<a href='{{ MEDIA_URL }}{{filepath}}' download={{filepath}}>download</a>

Leave a comment