Django cannot find static image file
I have a Django project served on AWS EC2, with just one HTML page which is supposed to display one static image (im.jpg)
but it doesn't. It does display body text on HTML file (following) but not the image. It says: "'im.jpg' could not be found" and returns:
"GET /static/im.jpg HTTP/1.1" 404 1837
Here's show.html:
<!DOCTYPE html>
<html lang='en'>
{% load static %}
<head> <title>Title </title> </head>
<body>
body text
<img src="{% static '/im.jpg' %}">
</body>
and my settings.py includes:
STATIC_URL = 'static/'
STATICFILES_DIR = [ os.path.join(BASE_DIR, 'static') ]
I have tried all different correct paths to im.jpg, even absolute path inside STATICFILES_DIR and rerun the server, but no success. Seems like Django cannot find the image file.
Adding a STATIC_ROOT in settings.py also DEBUG=False makes no difference.
Use the correct variable name
You wrote:
STATICFILES_DIR = [ os.path.join(BASE_DIR, 'static') ]
It should be:
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
Remove the extra slash in your HTML
You wrote:
<img src="{% static '/im.jpg' %}">
It should be:
<img src="{% static 'im.jpg' %}">
I hope this has been helpful.