【问题标题】:Django static files are not loading with NGINXDjango 静态文件未使用 NGINX 加载
【发布时间】:2020-07-18 22:06:09
【问题描述】:

我正在尝试使用 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'

我该如何解决?

【问题讨论】:

    标签: python django collectstatic


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 2016-08-15
      • 2017-03-15
      • 2021-11-20
      • 2017-12-06
      • 1970-01-01
      • 2019-08-31
      • 2014-11-12
      • 2021-04-03
      相关资源
      最近更新 更多