Hey there!
Sorry
for the bother, but I'm running into some issues dealing with static
files for a new site I'm building with Django. I'm new to django, but I
swear I went through at least 5 hours of StackOverflow comments and
other guides before hitting up this group. Anyway:
# The issue:
Static
files are not being served correctly, even though they are correctly
being collected using the `./manage.py collectstatic`, and placed in a
`static/` directory under the project root.
# Debugging information
OS: Ubuntu 16.04 (4.4.0-31-generic)
Nginx (reverse proxy and serving static file): 1.11.2
uWSGI (application server): 2.0.13.1
Django: 1.9.8
# Where my debugging left me
In
my nginx logs I'm finding requests that correctly get through to uWSGI
(page returns fine, though without static assets), where going manually
to the URI (
www.example.com/static/home/global.css) returns a 404 error.
The nginx error log for the request in question:
[date_stuff] [error] [log_stuff]: *1 open() "/server/website/<project_name>/static/home/global.css"
failed (2: No such file or directory), client: [redacted], server:
[redacted], request: "GET /static/home/global.css HTTP/2.0", host:
"[redacted]", referrer: "https://www.[redacted]"
Some things that I noticed:
I don't know how, but the request sent to nginx for the
global.css asset is trying to get it from `/server/website/<project_name>/static/home/global.css`, instead of `/server/website/static/home/global.css`.
I'm
not sure at that point whether it's an nginx problem or a django one,
but for some reason the `<project_name>` is being added, and nginx
is trying to fetch the static files from the wrong place.
# Project overview
Server/ <----- Located at the root of the machine at /server
---- Vagrantfile
---- db/
---- letsencrypt/
---- nginx/
---- uwsgi/
---- website/ <--- Project root
---- <project_name>/
---- home/ <--- home app
---- templates/
---- static/
---- admin/
---- home/
---- global.css <--- collected from the static dir in the `home` app
# <project_name>/settings.py config:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.static',
],
},
},
]
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static/') <------ I've tried this and just hardcoding
STATIC_ROOT = '/server/website/static/'
# Nginx server config:
location /static {
alias /server/website/static; <---- I've tried this and setting `root /server/website`
Cheers and thanks for any help guys (and sorry for the bother on such a beginner question),
Jason