Forums

loading static images

For loading images, I'm currently loading images in 2 places, and I'm hoping there is a way to only load it through the admin app.

Right now, I first have to load the file into the filesystem located at /static/img/blogposts and then I load the image as a field to my Blog Model through the admin app:

class Blog(models.Model):
    title = models.CharField(max_length=30, unique=True)
    headline = models.CharField(max_length=100)
    body = models.TextField()
    post_date = models.DateField(db_index=True, auto_now_add=True)
    category = models.ForeignKey('Category')
    views = models.IntegerField(editable=False, null=True, blank=True)
    image = models.ImageField(upload_to=get_image_path)
    active = models.BooleanField()

Is there a preferred way for me to skip the step of having to manually load the file into the filesystem and have the django admin app handle that step for me? Thanks.

I thought the django admin app did do exactly that? What happens if you just upload the image via the admin, without manually putting it into /static/ first?

I thought that's what I was reading as well. When I load it through admin but not load it manually into the file directory it can't find the image file and displays the general error image when it can't find the file. As soon as I load the file into the file directory it works fine without changing any code.

Is it possible that the images are being uploaded somewhere unexpected? A look at the docs for FileField (which defines the upload_to parameter that the ImageField inherits) says that the path you provide from your get_image_path "will be appended to your MEDIA_ROOT setting to determine the value of the url attribute". So perhaps it's being put into some subdirectory of something?

Sorry for the late response. Thanks for the suggestions - yes, that makes sense now, it was going into the media root instead of the intended static root. Thank you!