可以通过CSV文件上传到ImageField的图像
我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下:
或
我认为您只需要提供要上传的图像的路径即可。
在您的模型中,您具有这样的imagepath,and upload_to这样:
Try this:
Or
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: