Search code examples
pythondjangocollectstatic

Django static files are not loading with NGINX


Im trying to publish my first Django App on production Server with NGINX.

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

nginx configuration

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;
    }
}

In my settings I have the folow code

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',
]

an in urls.py I added

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

But, whe I run collectstatoc on production Server, I get this Error:

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

How can I fix it?


Solution

  • BASE_DIR is just a string ... strings have no path attribute... I think you meant

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