How to resolve static don't found on hosting django - python

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:

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')
]

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')

django static files getting a 404

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,
)

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

Django - Static Files from App Directories

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.

Categories