0

My database images get deleted from Heroku but the logo is not deleted. I use static to display the logo in this form :

<img src="{% static 'img/techshelta_logo.png' %}">

but my database images are displayed without using static:

<img src="{{ object.image.url }}" class="img-fluid" alt="object.title">

Is that the reason why Heroku deletes them?

NOTE When I try to use static as in the example below to display the DB images locally they are not show

<img src="{% static 'object.image.url' %}" class="img-fluid" alt="object.title" > 
DMT
  • 57
  • 2
  • 9
  • 1
    Those are uploaded images, not static files, so using `{% static %}` makes no sense. Your static files are part of your code repository (like your logo), your uploaded images are saved to some storage. What is your media storage? You should not save them locally (to disk) because Heroku deletes the local storage every time you update your application and every time it puts your code on a different instance. Use a 3rd party cloud storage provider like Amazon S3 or Cloudinary. – dirkgroten Jan 07 '20 at 15:29

1 Answers1

1

These three lines mean three different instructions in my opinion.

Instruction 1

<img src="{% static 'img/techshelta_logo.png' %}>"

This means to load the techshelta_logo.png from the img subfolder of the static directory. This should work always as the http server is unlikely to delete on its own under normal circumstances

Instruction -2

 <img src="{{ object.image.url }}" class="img-fluid" alt="object.title">

This instruction means that you are passing object from your view to template. The object has an attribute image which has a field url. Since this is stored in database as the full location/path where the image is saved, this is likely to work as well.

Instruction 3

<img src="{% static 'object.image.url' %}" class="img-fluid" alt="object.title" > 

'object.image.url' is most likely storing the full path of the image. When you are putting this instruction after static tag then the static path (defined in SETTINGS.py) is prepended to the already full path given by 'object.image.url'. The net result is a path that does not exist and thus the image can't be shown.

Having said all these I don't know why Heroku is deleting the images. Could be some instructions that are missing your attention that are getting passed to the database or it could be something subtle. I can't comment on that.

Hope this helps. If further clarifications are needed please let me know.

Amit
  • 2,018
  • 1
  • 8
  • 12
  • 1
    Uploaded files are never stored in the database. They are stored by the `DEFAULT_FILE_STORAGE` which by default is the `FileSystemStorage` at the location `MEDIA_ROOT`. – dirkgroten Jan 07 '20 at 15:49
  • Yes but Object.image.url stores the location in the database. The image itself is stored wherever the media directives are. I might be wrong but this is my guess without knowing the model structure of the app. – Amit Jan 07 '20 at 15:54
  • 1
    correct, but you say "this is likely to work as well" and the question is why does it **not work**, and why heroku deletes the images. Exactly that part you don't answer. The reason is that it deletes everything stored on disk every update. – dirkgroten Jan 07 '20 at 15:56
  • @dirkgroten You are right. The primary concern was to help solve the problem. 'Why Heroku deletes' was a subquestion in my opinion. I thought the main response should be to help him know why certain commands are working and why others are not. Yes you are right I did not answer the subproblem why Heroku deletes. You have effectively answered that. My understanding about the requirements, however, can be wrong. – Amit Jan 07 '20 at 16:02
  • OP starts by showing how he/she correctly displays static images and uploaded images (which works initially). Then asks why the uploaded images get deleted by Heroku, so that they don't show up eventually. Finally as a note (which can hardly be seen as the main question) OP mentions he/she tried the {% static %} approach. Your explanation helps with explaining why the code in the note doesn't work. – dirkgroten Jan 07 '20 at 16:10
  • @dirkgroten It is really strange that the question ended with 'Please help me resolve the problem.' This statement is not there anymore. Had it been there, then in my opinion that would be the summary of the purpose of the post. – Amit Jan 07 '20 at 16:15
  • no worries, anyway this is a duplicate and will be closed soon. – dirkgroten Jan 07 '20 at 16:17
  • @dirkgroten thank you for your explanations. They help greatly. I was able to upload the images and statics files to S3 so my images are no longer deleted. However, when the app is in debug mode, the static files work well, but when I turn off debug heroku complains of not getting entry to the static files. Please do you know what could be wrong?. Thank you – DMT Jan 14 '20 at 12:23
  • Issue Files are uploaded to the app but then disappear or are deleted after a while. Resolution The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy. This is similar to how many container based systems, such as Docker, operate. In addition, under normal operations dynos will restart every day in a process known as "Cycling". – Seth Apr 10 '20 at 12:26
  • These two facts mean that the filesystem on Heroku is not suitable for persistent storage of data. In cases where you need to store data we recommend using a database addon such as Postgres (for data) or a dedicated file storage service such as AWS S3 (for static files). If you don't want to set up an account with AWS to create an S3 bucket we also have addons here that handle storage and processing of static assets https://elements.heroku.com/addons – Seth Apr 10 '20 at 12:26