2

I deployed my Django app on heroku. Every thing is working fine except displaying images. Any uploaded image is not displayed if DEBUG=False.

settings.py

DEBUG = False

ALLOWED_HOSTS = ['salma-blog.herokuapp.com']

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.dirname(myblog.__file__)+'/static/'
STATIC_URL = '/static/'

#upload images
MEDIA_ROOT= os.path.dirname(myblog.__file__)+'/static/myblog/images'
MEDIA_URL='/images/'

urls.py

urlpatterns=[
   ...
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

image tag in my template

<img alt="img" src="/blog{{image}}"></a>
Salma
  • 133
  • 1
  • 10
  • Is the image being displayed with DEBUG=FALSE on your local **python django server** ?? – Ankush Raghuvanshi Sep 28 '16 at 06:49
  • no, on heroku server – Salma Sep 28 '16 at 06:51
  • So you mean to say that the image is being displayed in case of your local python django server with DEBUG=FALSE but not getting displayed on Heroku Server with DEBUG=FALSE? Or is it not being displayed anywhere, be it heroku or local python Django server?? – Ankush Raghuvanshi Sep 28 '16 at 06:55
  • the image is not being displayed if DEBUG=FALSE whether I am using my local python server or Heroku server – Salma Sep 28 '16 at 07:02
  • Just a quick reminder, if your repository weights more than 300 MB (or so) your pictures will be automatically deleted. It's all in their docs. If you have an app that will store a lot of images consider using AWS S3 in your django. – sebb Sep 28 '16 at 07:08
  • Thank you. When DEBUG=True. images are displayed correctly, so I think the problem is not about the Heroku file system. – Salma Sep 28 '16 at 07:12
  • Exactly. Its not related to Heroku. See this -> `http://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail` and this->`http://stackoverflow.com/questions/6405173/static-files-wont-load-when-out-of-debug-in-django` – Ankush Raghuvanshi Sep 28 '16 at 09:13

1 Answers1

0

Whitenoise uses static file finders when DEBUG=True, so you most likely have a problem with your static file collection process.

Update your STATIC_ROOT and MEDIA_ROOT settings to a recommended method, such as:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'images')
Selcuk
  • 57,004
  • 12
  • 102
  • 110