1

My Django application:

In my model, I use ImageField to allow the upload of images.

In my csv file I have an image path for an image like this: C:/Users/blah/DjangoProject/abc/abc/csvs/photos/product_1/product-1_1.jpg

In my csv's view I got:

Product.objects.create(product_name=row[0],
                       slug=row[0].lower().replace(' ', '-'),
                       product_description=row[1],
                       price=row[7],
                       image_1=ImageFile(open(row[8], 'rb')))

The row[8] contains the image's path.

When I'm going to my csv.html using Django's FileField to upload csv file which contain the image's path and product's info, after uploading the product should be created in my database which image should also be uploaded to a filesystem - not storing in database. Instead, I got this error:

OSError at /csvs/ [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\Users\blah\DjangoProject\abc\abc\media\photos\products\C:'

When I removed the image_1 row in Product.objects.create(), the product was uploaded and added to the database successfully. If I restore the image_1 row in the object creation, I got the same error again.

Does anyone know why?

1 Answers1

0

Try this:

Product.objects.create(product_name=row[0],
                       slug=row[0].lower().replace(' ', '-'),
                       product_description=row[1],
                       price=row[7],
                       image_1=row[8])

Or

from django.core.files import File; Product.objects.create(.....,image_1=File(file=open((row[8], 'rb')))

I think you just have to provide the path of the image to be uploaded. And in your model you have the ImagePath with and upload_to like this:

image_1 = models.ImageField(upload_to="media/styles/")
Sardar Faisal
  • 594
  • 5
  • 20
  • image_1=row[8] -- row[8] got image's path. My Product's model got this: class Product(models.Model): product_name = models.CharField(max_length=255, unique=True) slug = models.SlugField(max_length=255, unique=True) product_description = models.TextField(max_length=2500, blank=True) product_additional_description = models.TextField(max_length=2500, blank=True) image_1 = models.ImageField(upload_to='photos/products', blank=True, max_length=2500) -- but the error occurred. – Vinh Nguyen May 24 '22 at 07:33
  • from django.core.files import File; Product.objects.create(image_1=File(file=open((row[8], 'rb'))) – Sardar Faisal May 24 '22 at 07:38
  • If not using row[8] and instead of providing the path directly I got this error: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\blah\\DjangoProject\\abc\\abc\\media\\photos\\products\\C:' – Vinh Nguyen May 24 '22 at 07:39
  • Sardar Faisal, I tried your solution I got this error -- OSError at /csvs/ [WinError 123] The filename, directory name, or volume label syntax is incorrect: Add image directly in Django's admin panel will work for sure. Just not when trying to create an object using image's path. – Vinh Nguyen May 24 '22 at 07:44
  • https://stackoverflow.com/a/70544609/1568464 – Sardar Faisal May 24 '22 at 07:52
  • My core's settings file used to have this: MEDIA_ROOT = BASE_DIR / 'media', but followed the link you provided I got this now: MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/'). Of course I also got this already: MEDIA_URL = '/media/'. I also got this already in core's urls.py which is:+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT). As followed the instruction on the link you gave -- the error I still got is OSError at /csvs/ [WinError 123] The filename, directory name... is incorrect. – Vinh Nguyen May 24 '22 at 08:16
  • No ideas. Try to search the error. Make sure you have done makemigrations and migrate and all those thing. – Sardar Faisal May 24 '22 at 08:37
  • Everytime I make change to the model I would definitely do python manage.py makemigrations and migrate. I've been searching for a solution for couple hours, but none to be found. – Vinh Nguyen May 24 '22 at 08:48