Django: Does Imagefield Need A Filepath Or An Actual Image Object?
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 D
Solution 1:
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'))
Post a Comment for "Django: Does Imagefield Need A Filepath Or An Actual Image Object?"