Phil Gyford
Phil Gyford

Reputation: 14594

Using Django's ImageField, how to store files in STATIC_ROOT?

Using Django's ImageField(), how would I store the image file in STATIC_ROOT rather than MEDIA_ROOT?

I realise this is a bad idea for images which are uploaded through the admin, but these images are stored with the rest of the code in git, and attached to the objects when the site is set up using a script which imports all the objects' data. The images are not added/edited/deleted after this.

Upvotes: 2

Views: 1690

Answers (1)

Evans Murithi
Evans Murithi

Reputation: 3257

You can import FileSystemStorage and specify your custom upload path and pass the FileSystemStorage object as an argument in the ImageField()

from django.core.files.storage import FileSystemStorage
from django.conf.settings import STATIC_ROOT


upload_storage = FileSystemStorage(location=STATIC_ROOT, base_url='/uploads')


class ABCModel(models.Model):
    ...
    image = models.ImageField(upload_to='/your_image_name', storage=upload_storage)

References:

Upvotes: 4

Related Questions