I am constantly getting 404 error when my page is trying to load static image file.
ERROR:
[12/Aug/2022 12:02:54] "GET /myApp/ HTTP/1.1" 200 370 [12/Aug/2022 12:02:54] "GET /static/myApp/images/cofee.jpg HTTP/1.1" 404 1825
SETTINGS:
BASE_DIR = Path(__file__).resolve().parent.parent
my_templates = Path(BASE_DIR, 'templates')
static_dir = Path(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
static_dir,
]
HTML:
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>We are here to Help</h1>
<img src="{% static 'myApp/images/cofee.jpg' %}" alt= " no show ">
</body>
</html>
NOTE: server is running without error and page is loading just fine with heading1 showing but static image is not showing.
CodePudding user response:
change your setting and first import os like:
import os
and use this code as well:
STATIC_URL = '/static/'
# Add these new lines
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'YourAppName/static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')