YU.MO
YU.MO

Reputation: 35

the django + nginx can't load staticfiles

recently when i use the { %load staticfiles % } in my django app.i just found that it shows like this picture as follows: enter image description here

。the settings files are like this:

STATIC_DIR = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

and the file's directory are like follows:

web1->web1->settings.py
we1->static->img->ocen.jpg

the base.html are like this:

<!DOCTYPE html>

{ % load staticfiles % }

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<img src="{ % static "img/ocen.jpg" % }" alt="show picture">

</body>
</html>

the ngix configure file are like this:

location /static/ {
        alias /home/yumo/web1;
    }

To find the reason ,I try as follows:first , i konck :domainname/static/img/ocen.jpg in the browser. I can see my ocen.jpg in the browser. second: i just create the django app in my local virtual machine without using uwsgi and nginx. i can use the { % load staticfiles %} normally to achieve my aims. i just sincerely wan't someone give me some useful advise,thank you!

Upvotes: 1

Views: 91

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

In django template tags look like this {% tag %} Please note that there is no space between { and %.

So instead of

{ % load staticfiles % }
<img src="{ % static "img/ocen.jpg" % }" alt="show picture">

you should do:

{% load staticfiles %}
<img src="{% static 'img/ocen.jpg' %}" alt="show picture">    

Upvotes: 2

Related Questions