Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django FileSystemStorage.url is wrong

Code:

company = Company.objects.get(pk=pk)

    if request.POST:
        company_name = request.POST['company_name']

        company_logo = request.FILES['company_logo']
        fs = FileSystemStorage(location='/home/ubuntu/mywebsite/media/company/' + str(company.pk) + '/')
        filename = fs.save(company_logo.name, company_logo)
        uploaded_file_url = fs.url(filename)

fs.url returns: /media/thefilename.png which is wrong ... apparently the .url method doesn't take in to account what you've set your location attribute to?

How do I ensure that the correct path is being returned?

like image 335
dcolumbus Avatar asked Nov 23 '25 23:11

dcolumbus


1 Answers

Ran into the same problem, so to complet what @alfonso.kim said, I had to specify both base_url and location. with your code it would be something like this

company = Company.objects.get(pk=pk)

    if request.POST:
        company_name = request.POST['company_name']

        company_logo = request.FILES['company_logo']

        dir_storage = '/home/ubuntu/mywebsite/media/company/' + str(company.pk) + '/'
        fs = FileSystemStorage(location=dir_storage , base_url = dir_storage )
        filename = fs.save(company_logo.name, company_logo)
        uploaded_file_url = fs.url(filename)

Old thread, but hope it'll help

like image 80
will.mendil Avatar answered Nov 26 '25 11:11

will.mendil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!