Ad

No Static Files On Static Page In Django

- 1 answer

I have a static website in django at: domain.com/press/. The page at this address is visible, but there is a problem with static files - they don't appear.

Error in console (F12):

domain.com/press/%7B%%20static%20'images/presslogo.png'%20%%7D Failed to load resource: the server responded with a status of 400 (Bad Request)

My settings in nginx:

location /press/ {
    alias /path_to_templates/;
    index press.html;
}

Part of my html code:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Press</title>
</head>
<body>
<img src="{% static 'images/presslogo.png' %}">
</body>
</html>

setting.py

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

How can I display an image on a static page?

Ad

Answer

/press/ is being served by nginx. Hence <img src="{% static 'images/presslogo.png' %}"> will not work.

Please give exact path of your image. That is


<img src="domain.com/<your-static-url>/images/presslogo.png">

Ad
source: stackoverflow.com
Ad