I deployed my final working project to heroku.
The problem is only html content loading, Static files are missing. I tried lots but missing something. Can you spot the error.
# .. settings.py
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
)
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(BASE_DIR, 'static/'),
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
You must give the STATIC_ROOT ...(It can not be empty). Because the "HEROKU", always run 'collectstatic' while deploying.. and it creates the folder like as 'static files' and all the process running by the way of this folder contents.
Related
Hello I'm very confused about setting static files up. Every thing works fine(displays image, javascript, css) no matter what I try. So I'm confused which one is the right one.
Currently, this is how my project looks like
project
--project
---------static
---------media
--env
--static
--------media
--------static
And this is my code
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static")
# 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',
)
When I do python manage.py collectstatic, I don't get any error but static folder that's in outer static folder doesn't contain anything. but media folder that's in static folder contains the files in media folder that's in project folder.
Also
I have this for aws,
AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = True
DEFAULT_FILE_STORAGE = 'project.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'project.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'realproject'
S3DIRECT_REGION = 'ap-northeast-2'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + 'static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
import datetime
date_two_months_later = datetime.date.today() + datetime.timedelta(2 * 365 / 12)
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")
AWS_HEADERS = {
'Expires': expires,
'Cache-Control': 'max-age=86400',
}
Can someone please tell me if I'm doing it right?
by the way, I read https://docs.djangoproject.com/en/1.9/howto/static-files/
and followed it, i'm not sure if I followed it right(displayed above) which is why I'm asking.
The python manage.py collectstatic command looks for all your static directories and combines those file in the directory defined by the STATIC_ROOT setting.
In your case, STATIC_ROOT is set to os.path.join(os.path.dirname(BASE_DIR), "static", "static"), i.e.
your_project/static/static
So this is where the static files are being collected to. If you want them in the outer static directory, you can change STATIC_ROOT to os.path.join(os.path.dirname(BASE_DIR), "static").
There is a good discussion of this in the excellent Django docs here.
There is quite a lot to take in in these settings, so here is a quick summary of each static setting as an example:
# this is the URL that django will look for static resources at
# - i.e. http://your_domain/static
# so this one is a URL used when by your web server and in template
# shortcuts.
STATIC_URL = '/static/'
# this is where Django will look for static files to collect.
# I.e. the search locations that collectstatic uses.
# 'my_project/static' in this instance. You need to add the places
# you write your static files to this directory. For example, if you
# have several places where you are writing css files, add their
# container directories to this setting.
# it is a list of places to look for static files.
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
# this is where collectstatic will collect the static files to.
# When you hook this all into your webserver, you would tell your
# webserver that the /static/ url maps to this directory so that
# your app can find the static content. It's a directory in your
# project usually.
# it's a single directory where the static files are collected together.
STATIC_ROOT
I am trying to set up a Django 1.7 project to push to openshift. I'm following https://github.com/jfmatth/openshift-django17. I've sucessfully got the initial project going on openshift, now I'm trying to move a local project into this file structure so I can deploy it.
When I run the project I get:
TemplateDoesNotExist at /index/
The templates are all in the static/templates folder (in the screenshot). My settings url contains:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
......
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'wsgi','static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
How can I get help django find the templates?
move out your templates from /static/ in the parent directory, you don't want them moved to wsgi/static when doing collectstatic, then add this setting:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
We're using django-compressor and the django.contrib.staticfiles app and we're having problems while running the django development server and working on our SCSS: the wrong SCSS files gets compiled. The versions that are in STATIC_ROOT/app are getting found rather than the versions in app/static. This makes it so that edits to SCSS in app/static aren't reflected in the compiled CSS.
Removing everything in STATIC_ROOT/app fixes the issue, but it causes much confusion if collectstatic is executed for some reason.
Is there a way to ensure that the app/static files are compiled rather than any existing STATIC_ROOT/app files?
We're using django-compressor 1.4 with django 1.6 and the following settings are used in the django settings file:
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
'compressor.finders.CompressorFinder',
)
COMPRESS_PRECOMPILERS = (
("text/x-scss", 'sass --scss'),
)
STATICFILES_DIRS = [] #default
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
Your sass --scss comand in COMPRESS_PRECOMPILERS does not explicitly state a target directory. Hence the default is used, which seems to be stdin and stdout.
Now, compressor documentation is not so clear what using stdout means; but, from the examples it seems the files will end up in COMPRESS_ROOT (defaults to STATIC_ROOT/CACHE which in your case is root/base/static/CACHE/)
What I personally like is to explicitly state the in/out directories (to remain constant in different environments). Here is an example (using a pyScss compiler, but the idea is the same):
scss_cmd = '{python} -mscss -A "{image_output_path}" -a "{static_url}" ' \
'-S "{static_root}" -o "{{outfile}}" "{{infile}}"'.format(
python=sys.executable,
image_output_path=COMPRESS_ROOT,
static_url=STATIC_URL,
static_root=os.path.join(PROJECT_ROOT),
)
COMPRESS_PRECOMPILERS = (
('text/x-scss', scss_cmd),
)
(sorry if digging up long forgotten issues)
Use django-libsass:
COMPRESS_PRECOMPILERS = (
('text/x-sass', 'django_libsass.SassCompiler'),
('text/x-scss', 'django_libsass.SassCompiler'),
)
https://github.com/torchbox/django-libsass
Make sure to configure STATIC_URL and STATIC_ROOT correctly as described in https://docs.djangoproject.com/en/1.8/howto/static-files/.
For example:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
Compressor will take care of the rest, appropriately depending on the DEBUG variable.
It's weird that everytime I run collectstatic consecutively it copies the static files to an extra static folder within the static folder:
First time I run it:
Copying '/var/www/django/abc/public-www/static//images/coro.jpg'
the second time I run it, it will copy the files as below:
Copying '/var/www/django/abc/public-www/static/static/images/coro.jpg'
third time:
Copying '/var/www/django/abc/public-www/static/static/static/images/coro.jpg'
This is what I have in settings.py
PROJECT_ROOT = dirname(dirname(__file__))
VIRTUALENV_ROOT = dirname(PROJECT_ROOT)
STATIC_ROOT = join(VIRTUALENV_ROOT, 'public-www', 'static')
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
join(PROJECT_ROOT,'static'),
)
# 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',
)
I think I have found the culprit, I had this line for MEDIA_ROOT
MEDIA_ROOT = join(VIRTUALENV_ROOT, 'public-www')
Which should have been:
MEDIA_ROOT = join(VIRTUALENV_ROOT, 'public-www', 'media')
Once I changed it, collectstatic works as normal.
I want to be able to define my settings for static/media files using python to get the paths so I don't need different settings on my dev machine and my server.
So I have these settings;
import os
from unipath import Path
### PATH CONFIGURATION
# Absolute filesystem path to the top-level project folder
SITE_ROOT = Path(__file__).ancestor(3)
### MEDIA CONFIGURATION
MEDIA_ROOT = SITE_ROOT.child('media')
MEDIA_URL = '/media/'
### END MEDIA CONFIGURATION
### STATIC CONFIGURATION
STATIC_ROOT = SITE_ROOT.child('static')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
My problem is that locally it won't load the static files and the terminal says that STATICFILES_DIRS should not contain the STATICFILES_ROOT.
Is it possible to get Python to load the paths like this or am I wasting my time?
There's nothing wrong with your code per se, it's just that the point of the staticfiles app is to copy the files from the directories specified in STATICFILES_DIRS into the directory specified in STATIC_ROOT, so it doesn't make much sense to include the STATIC_ROOT directory in the STATICFILES_DIRS setting.
Unless you're actually using the staticfiles app with ./manage.py collectstatic, you may as well just leave the STATICFILES_DIRS setting empty, i.e. just change...
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
...to...
STATICFILES_DIRS = ()
Do like this:
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static/'),
)
That should work. Hope it helps!
+1 for both other answers. If you get tired of typing os.path.bla a lot here's a shortcut you can position at the top of your settings file (or import from anywhere else)
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
STATICFILES_DIRS = (
rel('static'),
)