15

On the heroku domain, I am not able to load my Media(Images Saved by using ImageField property) file images. However, I am able to see the images saved in the static field if I set debug = True

I save my images using the following command:

image = models.ImageField(upload_to=upload_location, null=True, blank=True, width_field="width_field", height_field = "height_field")

And I am able to reference them in my template by doing someething like this:

<img src="{{ instance.image.url }}" class="img-responsive">

Where instance is passed from my views.py like so:

instance = get_object_or_404(Post,slug=slug)
if instance.draft or instance.publish > timezone.now().date():
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
share_string = quote_plus(instance.content)
context = {
    "title": "Detail",
    "instance": instance,
    "share_string":share_string,
}
return render(request,"post_detail.html",context)

Thanks

1
  • You still didn't give any information about how you are attempting to serve those files. Commented Jan 4, 2017 at 22:33

2 Answers 2

17

Reason

Heroku runs your application on dynos and dynos go to sleep after 30 minutes if there is not request comes, this makes Heroku not preserve user upload media files between dynos restart.

Solution

Use Amazon S3 service for storing and serving media files + you can also serve static files from S3 also. for documentation refer here.

Sign up to request clarification or add additional context in comments.

1 Comment

Pointing to third party solutions should not be the accepted answer when there are solutions that don't require signing up for something else.
9

The Heroku filesystem is ephemeral, so dynos boot with a clean copy of the filesystem from the most recent deploy. Here are some ways to work around this:

  1. AWS S3
  2. If you don't want to set up an account with AWS to create an S3 bucket, Heroku has add-ons that handle storage and processing of static assets

For more details on this issue, see https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.