Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This backend doesn't support absolute paths #29

Open
coder-Aayush opened this issue Apr 15, 2021 · 3 comments
Open

This backend doesn't support absolute paths #29

coder-Aayush opened this issue Apr 15, 2021 · 3 comments

Comments

@coder-Aayush
Copy link

I was trying to upload image to cloudinary by decreasing the quality of image. I run into this error while uploading image.

Code

Models.py

class MyModel(models.Model):
      image = models.ImageField(upload_to='image/')
      .....

     def save(self, *args, **kwargs):
          instance = super(MyModel, self).save(*args, **kwargs)
          img = Image.open(self.image.path)
          img.save(self.image.path, quality=10, optimize=True)
          return instance
Logs
Traceback (most recent call last):
  File "venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/venv/lib/python3.9/site-packages/django/contrib/admin/options.py", line 614, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/venv/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "venv/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/venv/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 233, in inner
    return view(request, *args, **kwargs)
  File "/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1653, in add_view
    return self.changeform_view(request, None, form_url, extra_context)

    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/venv/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1580, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "venv/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1093, in save_model
    obj.save()
  File "/mymodels/models.py", line 33, in save
    img = Image.open(self.image.path)
  File "venv/lib/python3.9/site-packages/django/db/models/fields/files.py", line 57, in path
    return self.storage.path(self.name)
  File "/lib/python3.9/site-packages/django/core/files/storage.py", line 116, in path
    raise NotImplementedError("This backend doesn't support absolute paths.")

Exception Type: NotImplementedError at /admin/mymodels/add/
Exception Value: This backend doesn't support absolute paths.
@klis87
Copy link
Owner

klis87 commented Apr 15, 2021

@coder-Aayush according to this error, probably somehow you you absolute path, which is not implemented. Could you try using relative paths?

@awebisam
Copy link

awebisam commented Apr 16, 2021

I won't recommend saving images like this. What you are doing is, saving an image first and then accessing it to modify and save it again.
A better approach would be to intercept before saving the image. You can implement such like this.

Here I have also changed the image to jpeg to save more space but it's optional.

`

from django.db import models
from PIL import Image # pip install pillow
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile as imuf

class ImageModel(models.Model):
photo = models.ImageField(upload_to='path') # photo field

def save(self, *args, **kwargs):
  	# only compress on the first save()
    if not self.id: 
        self.photo = self.compressImage(self.photo)
    # Call the original save() method
    super(ImageModel, self).save(*args, **kwargs)

def compressImage(self, uImage):
  	# Use PIL to open
    new = Image.open(uImage)
    # Convert to RGB as RGBA can not be changed into JPEG(no transparency)
    new = new.convert('RGB')
    fName = uImage.name.split('.')[0]
    outio = BytesIO()
    # Save the new file
    new.save(outio, format='JPEG', quality=30)
    uImage = imuf(outio, 'ImageField', f"{fName}.jpg", 'image/jpeg', outio.tell(), None)
	# return that file name to store in Database
    return uImage`

@coder-Aayush
Copy link
Author

@coder-Aayush according to this error, probably somehow you you absolute path, which is not implemented. Could you try using relative paths?

Can you give some code sample?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants