I am using Django 3, and I have tried every solution available on the internet, but none of them worked for me. When I set DEBUG=False, I am unable to display an image on the HTML page. Here are my settings
-root_app
--main_app
---settings.py
---asgi.py
---urls.py
---wsgi.py
--sub_app
---admin.py
---urls.py
---views.py
---static
----style.css
---templates
----home.html
---media
----images
media path in settings.py
MEDIA_ROOT = os.path.join(BASE_DIR,'sub_app','media')
MEDIA_URL = '/media/'
Here what I have done to resolve it
- add context_processor
django.template.context_processors.media'
- add
+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in url_patterns ofurls.py
of sub_app - I changed static storage to
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
, otherwise I got 500 error. Check this comment
But still, I am unable to display the image at home.html
page.
When I click on image source using inspect element, I got this
/media/images/img.png