可以通过CSV文件上传到ImageField的图像

发布于 2025-02-01 07:53:20 字数 857 浏览 1 评论 0原文

我的Django应用程序:

在我的模型中,我使用ImageField允许上传图像。

在我的CSV文件中,我有这样的图像的图像路径: c:/users/blah/djangoproject/abc/abc/csvs/photos/product_1/product_1/product-1_1.jpg

在我的CSV视图中,我得到的:

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')))

行[8]包含图像的路径。

当我使用django的文件字段到csv.html上载包含图像路径和产品信息的CSV文件时,应在我的数据库中创建产品后,也应将其上传到文件系统中 - 不存储在数据库中。相反,我得到了一个错误:

Oserror在 /csvs / [WinError 123]文件名,目录名称或卷标签语法不正确:'c:\ users \ blah \ djangoproject \ abc \ abc \ abc \ abc \ abc \ media \ photos \ photos \ products \ products \ c:

' .Create(),将产品上传并成功地添加到数据库中。如果我还原对象创建中的image_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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪若未夕 2025-02-08 07:53:20

尝试一下:

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

来自django.core.files导入文件;
product.objects.create(.....,image_1 = file(file = open((row [8],'rb'))))

我认为您只需要提供要上传的图像的路径即可。
在您的模型中,您具有这样的imagepath,and upload_to这样:

image_1 = models.ImageField(upload_to="media/styles/")

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/")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文