简体   繁体   English

Django:ImageField需要文件路径还是实际的图像对象?

[英]Django: does ImageField need a filepath or an actual image object?

Running: Windows 7, Python 3.3, Django 1.6 运行: Windows 7,Python 3.3,Django 1.6

I'm confused as to how to store an image as part of a table in a Django database. 我对如何将图像存储为Django数据库中的表的一部分感到困惑。 There is a field called ImageField , and here are the Docs . 有一个名为ImageField的字段,这里是文档

Question: 题:

What should I pass to the ImageField when constructing the record: a filepath string? 在构造记录时我应该传递给ImageField:文件路径字符串? A url to an image? 图片的网址? Or a byte representation of the image itself? 或者图像本身的字节表示? Or is there some way of loading an image into Django and then just passing it a reference (or pointer) to that image object? 或者是否有某种方法将图像加载到Django中然后只是将一个引用(或指针)传递给该图像对象?

The docs say: "By default, ImageField instances are created as varchar(100) columns in your database.", which leads me to think that it wants a file path since it's asking for a string. 文档说:“默认情况下,ImageField实例在数据库中创建为varchar(100)列。”这使我认为它需要一个文件路径,因为它要求一个字符串。

The Docs also say that it "Requires the Pillow library.", which I've downloaded but it doesn't specify how to use Pillow for this. Docs还说它“需要枕头库”。我已经下载了它,但没有说明如何使用Pillow。

Background: I'm building a REST API through django to send and recieve log files from Selenium tests and store them in a SQL database. 背景:我正在通过django构建REST API,以便从Selenium测试中发送和接收日志文件,并将它们存储在SQL数据库中。 Each log file comes with a screenshot, hence the need for an ImageField. 每个日志文件都附带一个屏幕截图,因此需要一个ImageField。 Just started using Django a few days ago. 刚开始使用Django几天前。

Via imagefield and imagefield-derived forms you upload image to your MEDIA_ROOT. 通过imagefield和imagefield派生的表单,您可以将图像上传到MEDIA_ROOT。

Imagefield store its a path to image. Imagefield存储它的图像路径。 Check the 'media' part of django documentation, it describes how to store user uploaded images for example. 查看django文档的“媒体”部分,它描述了如何存储用户上传的图像。

Imagefield also define some basic proprieties of image like width and height. Imagefield还定义了图像的一些基本属性,如宽度和高度。

In your setting.py file you set: 在您的setting.py文件中设置:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Which said I want store uploaded files in yourproject/media and I want too show them in www.yoursite.com/media/path_to_image/image.png 其中说我想在你的项目/媒体中存储上传的文件,我也想在www.yoursite.com/media/path_to_image/image.png中显示它们

Then you can define a class with imagefield. 然后,您可以使用imagefield定义一个类。

class YourClass(models.Model):
    description = models.CharField(max_length=300, unique=True)
    picture = models.ImageField(upload_to='myimages')

So images will be stored in yourproject/media/myimages and availible at www.yoursite.com/media/myimages/image.png 因此,图像将存储在您的项目/媒体/ myimages中,并可在www.yoursite.com/media/myimages/image.png上获取。

To create new instance: 要创建新实例:

 from django.core.files import File
 from idontknow import YourClass

 yournewclass = YourClass(description='abc')

 yournewclass.picture.save('name.png',  File(open('path_to_pic/image.png', 'r'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM