django staticfiles collection error - python

I started a new project in django and created a development and a testing environment with their settings in the same folder as the custom settings.py everything is going well till I created a static file directory and I tried to collect static files but got error.
raise ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
this is my directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static"),
)
test_settings.py
# -*- coding: utf-8 -*-
from .settings import *
DEBUG = True
TEMPLATE_DEBUG = DEBUG
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}

STATIC_ROOT should be an absolute filesystem path:
STATIC_ROOT = os.path.abspath(os.path.join(os.path.dirname(BASE_DIR), 'static', 'static-only'))
Also, I would recommend keeping your STATICFILES_DIRS to directories inside your BASE_DIR (as part of your project/repository) rather than outside.

You should remove/comment STATICFILES_DIRS when running the ./manage.py collectstatic, and change STATIC_ROOT to STATIC_ROOT = os.path.join(BASE_DIR, 'static/static-only')

Related

how do I configure my staticfiles in django?

I followed the procedures to setup staticfiles but I'm not getting what i want
when I add an image to the section background it gives a wrong path "staticfiles/css/assets/img/oilcarriage.jpg"
instead of "staticfiles/assets/img/oilcarriage.jpg"
here's the path to the image:
i would be really grateful if you can help me now :)
You have to join the path so use the following STATICFILES_DIRS = (os.path.join(BASE_DIR, 'staticfiles'),)
Here you go static file config in settings.py file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

Heroku website shows Server Error (500) when opened

every time I try to open my website on heroku, it shows the error message
Server Error (500).
All my static files are correctly setup (I know this because I ran python3 manage.py collectstatic).
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
And my debug is set to false
I forgot to post an image of my logs, this is what it says;

How to resolve static don't found on hosting django

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:

Django static files and Heroku

In my main project directory I have a settings directory which has the following files: local.py, base.py, production.py and __init__.py. On running collectstatic files are saved in the folder staticfiles in the project directory
local.py
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
base.py
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
production.py
import os
from django.conf import settings
DEBUG = False
TEMPLATE_DEBUG = True
# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = '/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
When I run the following command:
heroku run python manage.py collectstatic --noinput
I am getting the error: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
A good template / guide for deploying on Heroku is here.
Take a look at their settings.py torwards the end::
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Don't use a relative path in the STATIC_ROOT in your production.py file.
Use it like your do in your base.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Unable to perform collectstatic

I am new to django ! When I use the command python manage.py collectstatic I get this error
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path
But I can successfully run the server .
My static files declarations are :
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets', os.path.join(PROJECT_DIR, '../static')),
)
and debug is set to true
DEBUG = True
How can I fix this? Else am missing any installation packages ?
Try this,
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
Look at https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATIC_ROOT
You must have to give path in STATIC_ROOT in settings.py where all your static files are collected as for example:-
STATIC_ROOT = "app-root/repo/wsgi/static"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets', 'app-root/repo/wsgi/openshift/static'),
)
you can create 'static' folder in any subfolder and have required files in it.
In settings.py add the following lines of code:
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'
After running
python manage.py collectstatic
a new static folder will be created in your parent App folder
well had this error as well. I fixed:
STATIC_URL = '/static/'
if DEBUG:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
else:
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'assest')
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')
]
I had to put STATIC_ROOT and STATIC_URL above the STATICFILES_DIRS declaration.
STATIC_ROOT = "/var/www/YourSiteFolder/static/"
STATIC_URL = '/static/'
look at https://docs.djangoproject.com/en/1.11/howto/static-files/#deployment
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
This works for me
if you want to load static files rather than admin panel files or getting errors while loading webpage static files like CSS js etc
I suggest you change the folder name of 'static' to 'staticfiles'
and then add this code in your settings.py
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
then after run python manage.py collectstatic
Then the problem will be fixed

Categories