Django:从服务器上的现有文件在模型中手动创建图像字段
这简直要了我的命!
我正在使用 django-filebrowser,我想创建一个图库应用程序,利用它的上传功能来管理图像。
我有一个图库模型,允许用户在服务器上选择或创建一个目录,并将文件上传到该文件夹以显示在图库中。我想自动搜寻用户上传图像并选择的目录,然后自动为文件夹中的每个图像创建 Image 实例。
class Gallery(model.Models):
gallerydirectory = FileBrowserField(...)
title = ...
description ...
class Image(model.Models):
image_field = models.ImageField()
问题是 FileBrowser 表示图像的方式与 Django 不同,但我想使用 DJango ImageFields,因为我可以在模板端使用其他应用程序(sorl 缩略图)。
我拥有该文件所需的所有数据,即文件名、路径等,我只是无法让 Django 创建 ImageField 的实例,而无需再次实际上传图像。我只是想填充它。
我在这里看到了另一个线程,它建议以下内容:
for image in filebrowser_image_objects_list:
f = File(open('path-to-file-on-server','r'))
i = Image()
i.image_field('filename.png',f.read())
但这是给我一个:
SuspiciousOperation error
Attempted access to '/filename.png' denied
这表明路径没有被正确读取。我已经打印了文件对象的属性,并且它正在打开正确的图像,它只是没有传递到 ImageField
帮助将不胜感激!
更新
我已经放弃尝试完成这项工作,因为它太混乱了。我上面遇到的问题是我将 ImageField 的 upload_field 设置为“/”,但它没有被注意到,这意味着该文件正在写入“/something.png”。
我对其进行了修改,以便图像现在也使用 FileBrowserField 而不是 ImageField。
This is killing me!
I'm using django-filebrowser, and I want to create a gallery app that leverages it's upload capabilities to manage images.
I have a Gallery model that allows the user to select or create a directory on the server, and upload files to that folder to appear in the gallery. I want to automatically trawl the directory that the user has uploaded images to and selected, and then automatically create Image instances for each image in the folder.
class Gallery(model.Models):
gallerydirectory = FileBrowserField(...)
title = ...
description ...
class Image(model.Models):
image_field = models.ImageField()
The problem is that FileBrowser represents images differently to Django, but I want to use DJango ImageFields as I can then use other apps (sorl thumbnails) on the template end.
I have all the data necessary for the file i.e. filename, path etc, I just can't get Django to create an instance of an ImageField, without actually uploading the image again. I simply want to populate it.
I have seen another thread here which suggests the following:
for image in filebrowser_image_objects_list:
f = File(open('path-to-file-on-server','r'))
i = Image()
i.image_field('filename.png',f.read())
but this is giving me a:
SuspiciousOperation error
Attempted access to '/filename.png' denied
which suggests that the path isn't being read correctly. I've printed the attributes of the File Object, and it is opening the correct image, it just isn't getting passed on to the ImageField
Help would be greately appreciated!
UPDATE
I've given up trying to get this work as it's just too messy. The problem I had above was that I had set the upload_field of the ImageField to '/' and it had gone unnoticed meaning that the file was being written to '/something.png'.
I've modified it so that the Image is now using a FileBrowserField as well instead of an ImageField.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能会遗漏一些东西,但这对我有用:
对于以下模型:
这样它不起作用:
它说:
用 Django 1.7、1.11 检查。
I might be missing something, but this worked for me:
for the following model:
This way it didn't work:
It says:
Checked with Django 1.7, 1.11.
我将此标记为已回答,因为这是执行此操作的正确方法:
以编程方式保存图像到 Django ImageField
I'm marking this as answered, as this is the correct way to do this:
Programmatically saving image to Django ImageField
这对我来说适用于 Python3 和 Django 2
This works for me on Python3 with Django 2