Storing images in a database using Django.

2,762 views
Skip to first unread message

Mlati Mudan

unread,
May 6, 2016, 3:06:08 PM5/6/16
to Django users

Hi guys, I'm a TOTAL noob when it comes to django and web development for that matter. I have an idea for a web app and I do have a basic understanding of programming. I've gone through a few Django tutorials and I'm confident I can do it, I just need help along the way :) 

So, my "brilliant" idea is to make an app for cooking for dummies. Basically, I'd have a repository of recipes. Each recipe consists of: recipe_ID, name, and 10 instruction-photo pairs ("now do this" + photo of that).

The photos are relatively lightweight (less than 50k each) so I think storing them in a database is the way to go. The only snag - I have absolutely no idea how to do it. I've googled a lot but I'm either too thick or my needs are too specific. 

I don't actually need to be able to store the images in the database through the web app. I was thinking of creating folders that contain the pertinent photos and "somehow" storing them in the database for further use. The name of the folder would be the same as the recipe_ID, and the photos would be named recipe_ID_01.jpg, recipe_ID_02.jpg, ...

Thanks in advance for any help you can give me :) 

André Machado

unread,
May 6, 2016, 4:06:06 PM5/6/16
to Django users
NEVER store images in DB, store its path so you can use it in a img tag later. =)

Alex Heyden

unread,
May 6, 2016, 4:43:16 PM5/6/16
to django...@googlegroups.com
There's an ImageField for use in models, but to really understand it, start with FileField


The general idea is that you have a directory configured by Django's settings that you can use to hold uploaded files. You can also use Django to serve these files if you want. (I tend to use nginx directly for this for performance reasons, but you do what works for you.)

I wouldn't go so far as to say that there's no use case for storing images in databases, but in general, André's advice is solid: store the path instead. Your OS is good at managing files. Let it do its job.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5dcad641-8837-477c-8a43-e7328c11850e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Adam Stein

unread,
May 6, 2016, 7:26:44 PM5/6/16
to django...@googlegroups.com
You can take a look at django-db-file-storage


In my case, I was making something for myself and the hosting server doesn't allow me access to any kind of file system, so I found this.

On Fri, 2016-05-06 at 15:42 -0500, Alex Heyden wrote:
There's an ImageField for use in models, but to really understand it, start with FileField


The general idea is that you have a directory configured by Django's settings that you can use to hold uploaded files. You can also use Django to serve these files if you want. (I tend to use nginx directly for this for performance reasons, but you do what works for you.)

I wouldn't go so far as to say that there's no use case for storing images in databases, but in general, André's advice is solid: store the path instead. Your OS is good at managing files. Let it do its job.

On Fri, May 6, 2016 at 2:53 PM, André Machado <csantos...@gmail.com> wrote:
NEVER store images in DB, store its path so you can use it in a img tag later. =)

Em sexta-feira, 6 de maio de 2016 16:06:08 UTC-3, Mlati Mudan escreveu:

Hi guys, I'm a TOTAL noob when it comes to django and web development for that matter. I have an idea for a web app and I do have a basic understanding of programming. I've gone through a few Django tutorials and I'm confident I can do it, I just need help along the way :) 

So, my "brilliant" idea is to make an app for cooking for dummies. Basically, I'd have a repository of recipes. Each recipe consists of: recipe_ID, name, and 10 instruction-photo pairs ("now do this" + photo of that).

The photos are relatively lightweight (less than 50k each) so I think storing them in a database is the way to go. The only snag - I have absolutely no idea how to do it. I've googled a lot but I'm either too thick or my needs are too specific. 

I don't actually need to be able to store the images in the database through the web app. I was thinking of creating folders that contain the pertinent photos and "somehow" storing them in the database for further use. The name of the folder would be the same as the recipe_ID, and the photos would be named recipe_ID_01.jpg, recipe_ID_02.jpg, ...

Thanks in advance for any help you can give me :) 



m1chael

unread,
May 6, 2016, 10:35:07 PM5/6/16
to django...@googlegroups.com

Storing images in a database will be loads of headache later. Like someone else said: store only the paths! Use FileField/imagefield

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Gabriel Marcondes

unread,
May 6, 2016, 10:59:29 PM5/6/16
to django...@googlegroups.com
If you can't store upload files on your file system (for example, if you deploy to Heroku), you can use something like django-storages [0] to upload them to a S3 bucket. S3 is cheap (you can start with a free trial, and after that you'll pay like 1 or 2 cents/month for small projects) and reliable.



For more options, visit https://groups.google.com/d/optout.



--
---
Gabriel Marcondes
Engenheiro de Computação - UFSCar
Reply all
Reply to author
Forward
0 new messages