[Answered ]-Django FileSystemStorage.url is wrong

2👍

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

0👍

according to the docs:

base_url

URL that serves the files stored at this location. Defaults to the value of your MEDIA_URL setting.

you are setting the location of the storage, while url is defaults to MEDIA_URL. If you want to serve user uploaded files check here and here.

hope this helps.

0👍

            //settings.py
            MEDIA_URL = '/media/'
            MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
            //end of settings.py

            //this one is for save the image to preferred directory
            logoRoot = os.path.join(settings.MEDIA_ROOT, 'images/')

            //this is for accessing the image after you save it
            logo_url = os.path.join(settings.MEDIA_URL, 'images/')

            logoRoot=logoRoot.replace("\\","/")


            fs = FileSystemStorage(location=logoRoot)
            filename = fs.save(company_logo.name, company_logo)


            //image url, don't use fs.url(filename) to get image url instead use code 
            below

            uploaded_file_url = logo_url+filename

Leave a comment