Django:ImageField是否需要一个文件路径或实际的图像对象? [英] Django: does ImageField need a filepath or an actual image object?

查看:249
本文介绍了Django:ImageField是否需要一个文件路径或实际的图像对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行: Windows 7,Python 3.3,Django 1.6



我很困惑,如何将图像存储为在Django数据库中的表。有一个名为 ImageField 的字段,这里是文档



问题:



构建记录时应该传递给ImageField的内容是:文件路径字符串?一个图像的网址?还是图像本身的字节表示?还是有一些将图像加载到Django中的方法,然后将它传递给该图像对象的引用(或指针)?



文档说:默认情况下, ImageField实例在数据库中被创建为varchar(100)列,这使我认为它需要一个文件路径,因为它要求一个字符串。



文件还说我需要枕头库,我已经下载了,但没有指定如何使用枕头。



背景:我正在通过django构建REST API,以从Selenium测试中发送和接收日志文件,并将它们存储在SQL数据库中。每个日志文件都带有截图,因此需要一个ImageField。刚刚开始使用Django几天前。

解决方案

通过imagefield和imagefield派生的表单,您将图像上传到您的MEDIA_ROOT。 p>

Imagefield存储其图像路径。检查django文档的media部分,它描述了如何存储用户上传的图像。



Imagefield还定义了一些基本属性,如宽度和高度。



在您的settings.py文件中设置:

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

哪个说我想要在你的项目/媒体上存储上传的文件,我想要在$ b中显示它们$ b www.yoursite.com/media/path_to_image/image.png



然后,您可以使用imagefield定义一个类。

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

所以图像将被存储在你的项目/媒体/ myimages,可以在
www.yoursite.com/media/myimages/image.png



创建新的实例:

从django.core.files导入文件
从idontknow导入YourClass

yournewclass = YourClass(description = 'abc')

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


Running: 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. There is a field called ImageField, and here are the Docs.

Question:

What should I pass to the ImageField when constructing the record: a filepath string? 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?

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.

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.

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. Each log file comes with a screenshot, hence the need for an ImageField. Just started using Django a few days ago.

解决方案

Via imagefield and imagefield-derived forms you upload image to your MEDIA_ROOT.

Imagefield store its a path to image. Check the 'media' part of django documentation, it describes how to store user uploaded images for example.

Imagefield also define some basic proprieties of image like width and height.

In your setting.py file you set:

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

Then you can define a class with 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

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

这篇关于Django:ImageField是否需要一个文件路径或实际的图像对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆