I've been trying to download a file through this view after users pay for it, but i can't seem to make it work. i first tried using file response
urls.py
urlpatterns = [
path('download-files/<int:id>/<str:order_id>/', download_file, name='download_file'),
]
models.py
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
price = models.DecimalField(max_digits=7, decimal_places=2)
file = models.FileField(upload_to="files", null=True)
views.py
def download_file(request, id, order_id):
obj = Product.objects.get(id=id)
order = Order.objects.get(order_id=order_id)
file = obj.file
response = FileResponse(open(file, 'rb'))
return response
But all i kept getting was this error
venv\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'bool' object has no attribute 'get'
I tried using boto3 to download the file
viewa.py
def download_file(request, id, order_id):
obj = Product.objects.get(id=id)
order = Order.objects.get(order_id=order_id)
file = obj.file
client = boto3.client('s3', aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID'), aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY'))
bucket_name = os.environ.get('AWS_STORAGE_BUCKET_NAME')
# url = 'https://' + settings.AWS_S3_CUSTOM_DOMAIN + '/media/' + f'{file.name}'
url = client.generate_presigned_url('get_object', Params = {'Bucket': bucket_name, 'Key': file.name, }, ExpiresIn = 600, )
return HttpResponseRedirect(url)
and i go this error
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>files/it_report_edited.docx</Key>
<RequestId>JR7XHE6A8D7F2872</RequestId <HostId>LiQyKEyHsw/xerU2lJXTEZG+m3obOoUdlVH06d2vE+KHHWQVspOqOb9mN9JmJqBW7DVb8Gc63uZmuLSVqcvbYO7SJ3GNLbzlvNnVAxLOvls=</HostId>
</Error>
It looks to me that i'm not giving the correct key that's why it can't find it, is there a coorect way to handle this? i've tried everything