Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store image files in Django?

Tags:

What is the typical scenario of storing image files in Django? More specifically, are images stored directly in a database blob (e.g. MongoDB GridFS), on a local filesystem or Amazon S3? For all three cases are there tools or django packages available to simplify your life with storing images?

I am currently saving images to and serving from the media folder in the Django project on a local development server. Someone told me it's a bad practice to serve static files on the same machine. So what's a typical real-world scenario of storing and serving static images in Django?

like image 884
Sahat Yalkabov Avatar asked May 02 '12 05:05

Sahat Yalkabov


People also ask

How do I store images in Django?

In Django, a default database is automatically created for you. All you have to do is add the tables called models. The upload_to tells Django to store the photo in a directory called pics under the media directory. The list_display list tells Django admin to display its contents in the admin dashboard.

Can we store images in Django database?

Most of the web applications deal with the file or images, Django provides the two model fields that allow the user to upload the images and files. These fields are FileField and ImageField; basically ImageField is a specialized version of FileField that uses Pillow to confirm that file is an image.

How can I pass an image to a template in Django?

you can pass your base_image to get_image. html page using dictionary content = {"base_image": base_image} and use {{base_image}} key at src attribute in get_img. html template to display.


2 Answers

A typical, real-world example would be to store uploaded images in a subdirectory of your site's media/ directory :)

This can become a problem if you need to store more images than your app server has hard disk space, or you need more than one app server, or you want to use a CDN to decrease your latency, or… one of a thousand other things.

But, unless you've got specific up-front requirements, the only way to know which (if any) of those thousand things you'll need to worry about is to get your site launched, and the fastest way to do that is to store images in a subdirectory of media/.

If you do this using a FileField, and you're careful in your code not to assume, for example, that the image is a file on your local disk (for example, you do use the .url() method of FileFile and you don't use the .path property), it will be straight forward to move those images onto a more appropriate backend when (and if) the time comes.

like image 98
David Wolever Avatar answered Oct 23 '22 12:10

David Wolever


By default, Django saves all of your files (and images) in the MEDIA_ROOT. You can however write a custom file storage, in order to save it on other places.

What to choose? It really depends.
How many files do you plan to store? How much storage will they use? How much bandwidth? Do you think there will be peaks of very high usage?

Usually, local file system is the fastest option; but if you see that those images will use too much of your bandwidth, then you may want to offload them, especially if the usage pattern sees high peaks. Conversely, if you need to scale out to several servers, then you may find it beneficial to move them in a database.

Anyway, you should decide only after getting some data, if possible real data. Switching to a different storage system is quite easy, so I would start with local file system, and then switch to something fancier only after seeing issues.

like image 40
rob Avatar answered Oct 23 '22 11:10

rob