I can't seem to get django static files working when I visit /static/accept.png it returns a 404. I have the file in project_folder/static
My installed apps has static files included
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
and this is what setting up the static files looks like
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_PATH = os.path.join(BASE_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATIC_URL = '/static/' # You may find this is already defined as such.
STATICFILES_DIRS = (
PROJECT_PATH + STATIC_URL,
)
Also I am trying to get this working locally i'm not worried about getting it to work in production yet.
You PROJECT_PATH is wrong. It points to one level up of the real project path.
Try these settings:
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
Don't get confused with PROJECT_PATH and BASE_DIR, if you want please print and get the values of both the variables, and get the clear picture that how will your code reach the "static" directory. Then try the code below
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
STATIC_PATH,
)
Related
Django does not load static on a site that is hosted (collectstatic did).I know that there are a lot of such questions, but I tried most of the solutions and did not help(I do not know what files to expose to understand the problem, so if you need more, I will expose)
settings.py(static part):
STATIC_URL = '/static/'
MEDIA_URL = "/media/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'/home/users/m/marselabdullin/caparol_center_spb_decision/static/',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
static folder on hosting:
I'm trying to deploy a django project to heroku (previously on openshift), but cannot get the static files right. The project structure is above. In firebug I'm getting:
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/css/bootstrap.min.css"
bootstrap.min.css
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/css/landing-page.css"
landing-page.css
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/jquery.js"
jquery.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/jquery-validate.js"
jquery-...date.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/bootstrap.min.js"
bootstrap.min.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/js/val.js"
val.js
"NetworkError: 404 NOT FOUND - https://myproject.herokuapp.com/static/font-awesome-4.2.0/css/font-awesome.min.css"
The abridged settings.py file:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
....
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
....
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'static/js'),
os.path.join(BASE_DIR, 'static/css')
)
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(BASE_DIR, 'templates'),
mysite/wsgi.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
What am I doing wrong?
I had a similar problem. Changing STATIC_ROOT to BASE_DIR (from PROJECT_ROOT) seemed to help.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_ROOT = '%s/site_media' % PROJECT_DIR
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(STATIC_ROOT, 'images'),)
When I run 127.0.0.1:8000/static/something.jpg everything works.
But I want get something.jpg like this 127.0.0.1:8000/static/images/something.jpg
When I change STATICFILES_DIRS:
STATICFILES_DIRS = ('',)
127.0.0.1:8000/static/images/something.jpg doesn't work, why?
Use
STATICFILES_DIRS = (os.path.join(STATIC_ROOT, ''),)
Your STATIC_DIR should point to the project site_media dir. When adding images' to it, then 127.0.0.1:8000/static/images/something.jpg is being searched in .../site_media/images/images/something.jpg
In a development environment, I'd like to use static files from the app directories.
#settings.py
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static/'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
#...
'django.core.context_processors.static',
#...
)
INSTALLED_APPS = (
#...
'django.contrib.staticfiles',
#...
)
I can find my static file if located in /static/css/file.css but not if in an_app/static/css/file.css.
Ok I found the problem, I made an oversight that was not visible in my question. I was searching into in static/js, static/flash, static/css, static/images and put the files directly into app/static so they were not found.
Check the value of STATIC_ROOT and STATICFILES_DIRS.
Add a print in your setting file.
I have the following Django project layout:
project_folder/
app_folder/
static/
folder1/
file1
templates/
...
compressor -> (symlink to django_compressor app with my modifications)
__init__.py
manage.py
settings.py
urls.py
From the templatetag specificed in my fork of django_compressor I'm calling a class that does the following:
class StorageMixin(object):
from django import VERSION as DJANGO_VERSION
if DJANGO_VERSION[:2] >= (1, 3):
from django.contrib.staticfiles.finders import find as _django_find
def _find_file_path(self, path):
return self._django_find(path)
else:
def _find_file_path(self, path):
static_roots = getattr(settings, 'STATIC_ROOTS', []) + [settings.COMPRESS_ROOT]
for root in static_roots:
filename = os.path.join(root, basename)
if os.path.exists(filename):
return filename
return None
So, if django is of the new version, it tries to use staticfiles finders, otherwise mimics its basic finder through the STATIC_ROOTS config variable.
Finally, the question: given the folders layout above, I pass "folder1/file1" to finders.find method, and I have the following settings:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'compressor',
'app_folder',
)
and
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
and I get None from the find call.
Any ideas?
UPD. More strange details: I ran
python manage.py shell
and did
from django.contrib.staticfiles.finders import find
find("folder1/file1")
And it gave me the correct result...
The answer was: import the whole module, not only the find method
I am using the static files with my new site and it took a while to get set up. Do you have something like this in your settings.py?
Also check out:
Django staticfiles app help
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/static/"
STATICFILES_ROOT = '/path/to/my/site/static/'
STATIC_ROOT = STATICFILES_ROOT
# URL that handles the static files served from STATICFILES_ROOT.
# Example: "http://static.lawrence.com/", "http://example.com/static/"
STATICFILES_URL = '/static/'
STATIC_URL = STATICFILES_URL
# URL prefix for admin media -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# A list of locations of additional static files
STATICFILES_DIRS = (
("game", BASE_DIR + "/game/media"),
("sitemedia", BASE_DIR + "/templates/media/"),
)
STATIC_DIRS = STATICFILES_DIRS
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
STATIC_FINDERS= STATICFILES_FINDERS