Search code examples
djangoheroku-postgres

My database images get deleted from Heroku after some time, but the logo which is not in the database is not deleted


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" > 

Solution

  • 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.