this is the admin page deployed in heroku,
enter image description here
here is my settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
deploy_settings.init.py
DEBUG = False
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
I tried to run python manage.py collectstatic locally, and run it in heroku bash also, but it didn't work. Do we actually need to run this command? or staticfiles are collected when pushing to heroku master?
I tried to add DEBUG_COLLECTSTATIC=1 in heroku config variables, but it doesn't work.
one last note, I tried to install whitenoise and add it to the settings.py middlewars, and add
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' in deply_settings.init.py but I recieved this error,
enter image description here
when
It is been fixed by removing this line in deploy_settings.init.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Related
My website was doing everything well and showing all the CSS until I ran collectstatic on it. Now everything is how it would look if CSS didn't exist. Is there any solution to this? Or is there some way I can delete the collectstatic to get back the previous thing?
I followed this tutorial to host this website: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
Here's my settings.py(only the last bit where I set the static and the media stuff):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATICFILES_DIRS = []
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
LOGIN_URL = '/main/user_login/'
Okay, so as you mentioned it stopped working after you ran collectstatic command.
collectstatic command makes Django looks for all static files in your apps and collects them in a single directory which is STATIC_ROOT. (In production it needs a single directory for all the static files)
Put the directories containing your static files into the STATICFILES_DIRS.
You also have to include your static urls in your urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [your paths go here]
urlpatterns += staticfiles_urlpatterns()
After including your static directories into the STATICFILES_DIRS array and including the static urls in your urls.py, use the command collectstatic and then it should work.
Got a Django project named django_server. When I run
python manage.py runserver
the page shows up as expected
Then, if I run
gunicorn django_server.wsgi:application --bind 0.0.0.0:8000
The page shows without styling
Checking the console, can see the following errors for both .css and .js files
The resource from
“http://0.0.0.0:8000/static/....css” was blocked
due to MIME type (“text/html”) mismatch (X-Content-Type-Options:
nosniff).
In the terminal where the gunicorn command was executed, can read
NOT FOUND: /static/rest_framework/css/bootstrap.min.css
NOT FOUND: /static/rest_framework/css/bootstrap-tweaks.min.css
...
In settings.py I mention
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
This is the folder structure
Checked the permissions in static folder (ls -l)and they show as
drwxrwxr-x 4 tiago tiago 4096 jun 2 15:49 static
Checking the permissions in the files where the problem happens and
Added also to settings.py
import mimetypes
mimetypes.add_type("text/css",".css",True)
mimetypes.add_type("text/javascript",".js",True)
But the error remains.
You need to run python manage.py collectstatic.
On your settings.py I recommend you to use whitenoise to serve your files.
1) pip install whitenoise
2) Add STATICFILES_STORAGE on settings.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
3) Add to your MIDDLEWARE on settings.py
`MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
...
]
I am trying to deploy an app using heroku. But for some reason, the static files are not loaded with whitenoise and there is an internal server error message in the browser :
Internal Server Error
The server encountered an unexpected internal
server error (generated by waitress)
In the heroku logs :
ValueError: The file 'css/style.css' could not be found with
whitenoise.django.GzipManifestStaticFilesStorage object at 0x7f5da0186750
Here are some points.
an empty static file (with an empty robots.txt in) is created in the root folder for heroku.
python manage.py runserver : everything works fine. no issue.
heroku local 500 error. (but the view still shows up)
css file path : myhellowebapp/collection/static/css/style.css
both python manage.py collectstatic and heroku run python manage.py collectstatic work just fine.
If I comment out STATICFILES_STORAGE =
'whitenoise.django.GzipManifestStaticFilesStorage',
the error message in the browser disappear and I can see html of the app in the browser, but no css file is attached
So it has to do with whitenoise but no I idea how to fix this.
setting.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
settings_production.py :
from hellowebapp.settings import *
...
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
wsgi.py :
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"hellowebapp.settings_production")
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
I've been working this problem for days I tried all the possible solutions that I found online but none of them worked. Can anyone help?
The link to all of my code :
https://github.com/ryuji-the-dragon/hellomyapp
I'm trying to push my Django project to Heroku, but it isn't loading the staticfiles.
I used this to setup the things, everything is fine but I'm not able to fix the issue with static files.
My directory structure is like this
help_the_needy
help_the_needy
__init__.py
settings.py
urls.py
views.py
wsgi.py
manage.py
Procfile
requirements.txt
static
css
font-awesome
fonts
img
js
templates
base.html
display_list2.html
index.html
Here is the complete code (all files).
This is my settings.py.
I tried alot of things to fix this, but nothing seems to work.
When I push it does copy static files but it's not loading them.
Can someone please point me to my mistake? Where is it wrong?
I have been dealing with the same problem too. And here are the 2 things that I changed in my code.
(I'm using Django 1.7)
1) settings.py
I add these lines to the setting files
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
# Add to this list all the locations containing your static files
)
STATIC_ROOT: this tells Django where to (a) put the static files when you run python manage.py collectstatic and (b) find the static files when you run the application
TEMPLATE_DIRS: this tells Django where to look for your static files when it search for statics files when you run python manage.py collectstatic
2) wsgi.py
Originally my file was:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And I changed it to:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxx.settings")
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Read here for more information on whitenoise: https://devcenter.heroku.com/articles/django-assets#whitenoise
Also, remember to install whitenoise:
pip install whitenoise==2.0.6
Before deploying the project, run:
python manage.py collectstatic
This will create a folder indicated by STATIC_ROOT (declared in your settings.py), containing all your static files.
Since it's been a few years since this was posted (and it still pops up when I search for the issue), here's what worked for me in Django 3.2.
pip install whitenoise
Make sure in settings.py you have
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Add whitenoise to your Middleware:
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
]
Make sure the directory you specified as the STATIC_ROOT (staticfiles) exists in your base directory and is not empty.
After that, I committed the changes and Heroku was able to build the static files and everything worked fine.
pip install whitenoise
Add whitenoise to requirement.txt.
and also add whitenoise in the middleware of settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', #add whitenoise
'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',
]
Follow the steps in https://devcenter.heroku.com/articles/django-assets
Your STATICFILES_DIRS setting is wrong. It should be pointing to the actual location of the "static" directory containing the files:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
It looks like django don't know where your static_root is.
This folder is autogenerated when you fire manage.py collectstatic
Make sure that the folders static and templates are inside your django app. You didn't created a django app to put this folders into. You created a django project the next step is to create a django app with something like this python manage.py startapp polls
# Absolute filesystem path to the Django project directory:
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
# Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT)
STATIC_URL = '/static/'
STATIC_ROOT = normpath(join(SITE_ROOT, 'static_root'))
Your urls.py file lacks the setting to manage and route requests for static resources.
in order to provide access to static resorces you must add to urlpatterns of urls.py:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
I have a Django app on Heroku. I am having some problems with static files (they are loading in one Heroku environment but not another), so I tried the debug command recommended here.
$ heroku run python manage.py collectstatic --noinput
Running `python manage.py collectstatic --noinput` attached to terminal... up, run.8771
OSError: [Errno 2] No such file or directory: '/app/{myappname}/static'
Here is my settings.py, which is the same thing Heroku recommends:
import os
import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I get the error whether or not I actually have a directory "static" at the root level in my Git repo (tested it both ways).
Any ideas?
It's looking for a folder named 'static' that's next to the settings.py, i.e. in the project folder, not at the root of the git repo.
git root/
git root/{app name}
git root/{app name}/settings.py
git root/{app name}/static/ <- this is what you're missing
Note that empty folders aren't tracked by git, so you'll have to put a blank file in there if it's empty. Alternatively, remove the STATICFILES_DIRS setting until you need it.
I just had this same problem, and here's the solution that worked for me:
I changed:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
to:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myappfolder/static'),
)
#joerick's answer above is the thing. However, if you do not want to place another 'static' folder (git root/{your app}/static), you might consider changing the BASE_DIR variable that is initially supplied by django-admin makeproject:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
which is just the (git root/) directory