I have a form containing an ImageField and a FileField
The files are uploading to the correct folders, however when I try to retrieve the url to display on screen it gives me an incorrect location
fs_img = FileSystemStorage(location='media/images/')
imageName = fs_img.save(image.name,image)
uploaded_image_url = fs_img.url(imageName)
E.G. Images upload as media/images/profile_image.jpg However when I try to retrieve the url of the file that just saved, in order to save the location to a DB, it retrieves it as media/profile_image.jpg which doesn't exist
I am aware that the default location used by FileSystemStorage is MEDIA_ROOT and that seems to be just what fs_img.url(imageName) is using where
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Also, I have found that in the models.py file using an upload_to setting has no effect
image = models.ImageField(
upload_to = 'media/images/',
default='media/no_image.png'
)
How do I get fs_img.url(imageName) to return the correct URL so that I can save it to my database?
I think i fixed this as follows:
settings.py
views.py
uploaded_image_url and uploaded_document_url values are now returning correctly