简体   繁体   English

Gunicorn Nginx Django static 文件:图像未显示

[英]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.出于某种原因,我页面上的CSS似乎加载得很好。

settings.py设置.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 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.我试过在其他目录等中安装 static 文件,看看有什么用,但我没有任何运气。 I had the static files in the project but after reading that's not a good practice I put them into var/www.我在项目中有 static 文件,但在阅读这不是一个好习惯后,我将它们放入 var/www。

can you add this into nginx conf and try:您可以将其添加到 nginx conf 中并尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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