简体   繁体   中英

Gunicorn Nginx Django static files: images not shown

I've been digging through articles and posts about this subject but can't seem to get my images to load. For some reason the CSS on my pages seems to load just fine.

settings.py

BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

also I tried

STATIC_ROOT = '/var/www/myname/'

template

<img src="{% static 'picture.jpg' %}" class="img-fluid">

NGINX

/etc/nginx/sites-enabled/django_project

server {
    listen 80;
    server_name mywebsite.com;

    location /static/ {
        alias /var/www/myname/static/;
    }
}

directories

/var/www/myname
└── static
    └── admin
        ├── css
        │   ├── styles1.css
        │   └── styles2.css
        ├── images
        │   ├── picture.jpg
        │   └── python.jpg
        └── js
            └── scripts.js

/home/myname/myprojectdir
├── django_project     
│   ├── django_project 
│   │   └── __pycache__
│   ├── etc            
│   ├── index                   
│   │   ├── migrations          
│   │   │   └── __pycache__     
│   │   ├── __pycache__         
│   │   └── templates           
│   │       └── index           
│   │           └── backups     
│   ├── __pycache__             
│   └── static                  
│       ├── admin               
│       │   ├── css             
│       │   │   └── vendor      
│       │   │       └── select2 
│       │   ├── fonts           
│       │   ├── images          
│       │   │   └── gis         
│       │   ├── img             
│       │   │   └── gis         
│       │   └── js              
│       │       ├── admin       
│       │       └── vendor      
│       │           ├── jquery  
│       │           ├── select2 
│       │           │   └── i18n
│       │           └── xregexp 
│       ├── images
│       └── index
│           ├── css
│           ├── images
│           └── js
├── myname_env
│   ├── bin
│   └── lib
│       └── python3.10
│           └── site-packages
└── static
    └── admin
        ├── css
        │   └── vendor
        │       └── select2
        ├── fonts
        ├── img
        │   └── gis
        └── js
            ├── admin
            └── vendor
                ├── jquery
                ├── select2
                │   └── i18n
                └── xregexp

I've tried installing the static files in other directories, etc, to see what works but I'm not having any luck whatsoever. I had the static files in the project but after reading that's not a good practice I put them into var/www.

can you add this into nginx conf and try:

location /images/ {
        alias /var/www/myname/static/images;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM