I\'ve seen several posts for this issue but didn\'t found my solution.
I\'m trying to serve static files within my Django 1.3 development environment.
Here a
If your static URL is correct but still:
Not found: /static/css/main.css
Perhaps your WSGI problem.
➡ Config WSGI serves both development env and production env
==========================project/project/wsgi.py==========================
import os
from django.conf import settings
from django.contrib.staticfiles.handlers import StaticFilesHandler
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
if settings.DEBUG:
application = StaticFilesHandler(get_wsgi_application())
else:
application = get_wsgi_application()
STATIC_ROOT
linestatic
folder in different directory. For suppose the directory is: project\static
Now update: STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'project/static/')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Whatever you do the main point is STATICFILES_DIRS
and STATIC_ROOT
should not contain same directory.
I know it's been a long time but hope the new buddies can get help from it
I confused STATIC_ROOT and STATICFILES_DIRS
Actually I was not really understanding the utility of STATIC_ROOT. I thought that it was the directory on which I have to put my common files. This directory is used for the production, this is the directory on which static files will be put (collected) by collectstatic.
STATICFILES_DIRS is the one that I need.
Since I'm in a development environment, the solution for me is to not use STATIC_ROOT (or to specify another path) and set my common files directory in STATICFILES_DIRS:
#STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/'))
import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static/'),
)
Also don't forget to from django.conf import settings
Another error can be not having your app listed in the INSTALLED_APPS
listing like:
INSTALLED_APPS = [
# ...
'your_app',
]
Without having it in, you can face problems like not detecting your static files, basically all the files involving your app. Even though it can be correct as suggested in the correct answer by using:
STATICFILES_DIRS = (adding/path/of/your/app)
Can be one of the errors and should be reviewed if getting this error.
STATICFILES_DIRS
is used in development and STATIC_ROOT
in production,
STATICFILES_DIRS
and STATIC_ROOT
should not have same folder name,
If you need to use the exact same static folder in development and production, try this method
include this in settings.py
import socket
HOSTNAME = socket.gethostname()
# if hostname same as production url name use STATIC_ROOT
if HOSTNAME == 'www.example.com':
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
else:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
{'document_root', settings.STATIC_ROOT}
needs to be
{'document_root': settings.STATIC_ROOT}
or you'll get an error like
dictionary update sequence element #0 has length 6; 2 is required