首页 > 解决方案 > Django 静态文件未使用 NGINX 加载

问题描述

我正在尝试使用 NGINX 在生产服务器上发布我的第一个 Django 应用程序。

project
--app1
--app2
--config
   settings.py
   wsgi.py
   urls.py
--venv
manage.py

nginx配置

server {
    listen 80;
    server_name my_IP_adress;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/websites/testAPP;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

在我的设置中,我有以下代码

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_DIR = environ.Path(__file__)


STATIC_URL = '/static/'
STATIC_ROOT = str(ROOT_DIR('statics'))
STATICFILES_DIRS = [
    str(BASE_DIR.path('static')),
]
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

我在 urls.py 中添加了一个

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

但是,当我在生产服务器上运行 collectstatoc 时,我得到了这个错误:

settings.py", line 24, in <module>
    str(BASE_DIR.path('static')),
AttributeError: 'str' object has no attribute 'path'

我该如何解决?

标签: pythondjangocollectstatic

解决方案


BASE_DIR 只是一个字符串...字符串没有path属性...我认为您的意思是

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static'),
]

推荐阅读