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.
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 have run
python manage.py collectstatic
so I have copies of my static files in "/static" folder.
But now Django uses files from /static/myapp/js/myapp.js, not from myapp/static/myapp/js/myapp.js
what should I change to resolve myapp/static/myapp/js/myapp.js first?
settings.py
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'
From the Django official documentation:
The Default value of STATICFILES_FINDERS is:
[
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). If multiple files with the same name are present, the first file that is found will be used.
So, in your settings.py, add this:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
]
Basically what we are doing is overriding STATICFILES_FINDERS by swapping the default entries, so that AppDirectoriesFinder is used first.
In my setting.py I have next code:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
And in views.py I want to get some of the images inside static folder as follows:
for f in ["static/img/logo/contact-form/logo_black.png", "static/img/logo/contact-form/logo_white.png":
fp = open(os.path.join(BASE_DIR, f), 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(f))
msg.attach(msg_img)
But I'm getting an error:
"[Errno 2] No such file or directory: '/Users/Admin/Projects/web-dealers/webDealers/static/img/logo/contact-form/logo-black.png'"
UPDATE
The urls.py is as follows:
urlpatterns = [
url(r'^django-admin/', admin.site.urls),
url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'^search/$', search_views.search, name='search'),
url(r'^api/', include('API.urls')),
url(r'^contact-us/', contact_us, name="contact_us"),
# Languages
url(r'^i18n/', include('django.conf.urls.i18n'), name='set_language'),
url(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog'),
# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r'', include(wagtail_urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
What am I doing wrong?
Static in django are always tricky and a hell. I do not know why:) Don't you have DEBUG=False and forgot to do python manage.py collectstatic?
However, this works for me:
settings.py:
INSTALLED_APPS = [
...some other applications....
'django.contrib.staticfiles',
...some other applications....
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
I do not have anything in urls.py. I can access static files normally like: e.g. localhost:8000/static/image.png.
EDIT:
It seems that I did not read that properly and answered something different:) Your problem is not during the static files serving but when you access internally at the backend. It says: file not found. Are you sure that the path is correct? Or, that the os.path.join(BASE_DIR', 'static') really contains the file? You try to access the file using and absolut path so it must be easily verifiable is it is there or not at:
'/Users/Admin/Projects/web-dealers/webDealers/static/img/logo/contact-form/logo-black.png'
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,
)
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