简体   繁体   中英

heroku-django-uploaded image is not displayed if DEBUG=False

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>

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM