I've deployed a Django 1.8 on heroku and none of the static files getting found at all. I can't figure out what I'm doing wrong:
# settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app123'
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
APPEND_SLASH = False
I put all the static files into myproject/app123/static/(css|images|js) on localhost. When I run heroku run python manage.py collectstatic it doesn't copy anything:
$ heroku run python manage.py collectstatic
Running `python manage.py collectstatic` attached to terminal... up, run.7135
You have requested to collect static files at the destination
location as specified in your settings:
/app123/staticfiles
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
0 static files copied to '/app123/staticfiles', 117 unmodified.
Related
I am trying to deploy my Django project on Digital Ocean. I created my droplet and spaces on Digital Ocean and created a static folder to store my static files. I pulled my code from my github-repo. then I installed all requirements and tried to collect static files with command
python3 manage.py collectstatic
but it shows
Unknown command: 'collectstatic'
Type 'manage.py help' for usage.
what should I do here?
I checked my manage.py helper but it has no command as collectstatic
check,
compilemessages,
createcachetable,
dbshell,
diffsettings,
dumpdata,
flush,
inspectdb,
loaddata,
makemessages,
makemigrations,
migrate,
runserver,
sendtestemail,
shell,
showmigrations,
sqlflush,
sqlmigrate,
sqlsequencereset,
squashmigrations,
startapp,
startproject,
test,
testserver,
these are the commands in manage.py helper.
And my settings.py is the following
import os
from pathlib import Path
from decouple import config
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = config('DEBUG', default=False, cast=bool)
SECRET_KEY = config("SECRET_KEY")
ALLOWED_HOSTS = ["134.209.153.105",]
ROOT_URLCONF = f'{config("PROJECT_NAME")}.urls'
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'crispy_forms',
'accounts',
'adminn',
'student',
'union',
'chat',
'channels',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
]
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = config('AWS_S3_ENDPOINT_URL')
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = config('AWS_LOCATION')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
TEMP = os.path.join(BASE_DIR, 'temp')
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
BASE_URL = "http://134.209.153.105"
here, in static url AWS_STORAGE_BUCKET_NAME, AWS_S3_ENDPOINT_UR, AWS_LOCATION are following...
AWS_STORAGE_BUCKET_NAME=studentcricle
AWS_S3_ENDPOINT_URL=https://sfo3.digitaloceanspaces.com
AWS_LOCATION=studentcircle-static
Thank you for those who checked my question.
My problem is solved as I run the following code.
export DJANGO_SETTINGS_MODULE=mysite.settings
I found it from Django documentation.
But I still did not find out what was the real problem.
It was something about my settings file. or multiple settings files
so, if anyone know the details, please describe it here. or in personal.
I think you should uncomment STATIC_ROOT inside your settings.py file and try this:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Try with replacing STATIC_ROOT in settings.py file with the following:
STATIC_ROOT = '/static/'
and whenever you run the python3 manage.py collectstatic command, make sure you are in the base directory where the manage.py file is located and there is a folder named static.
First of all please check if there is a folder in base directory named 'static'; if yes, then make a little change in the following code, remove'/' after static:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Secondly, make sure you have registered all the apps in the installed apps menu.
If the problem still persists, then try to run the following command: python manage.py shell
It should let you know where the problem is, if it's in the settings.
Hi I am trying to deploy django app on Google app engine. My Django app works fine in the locally but in google app engine it is not working. I checked and found that issue is with my static files. My static files are not getting loaded in app engine.
********.appspot.com/static/youtube/about.css
Not Found
The requested resource was not found on this server.
Its been two days I have trying to follow answers on various forums but none worked for me. I have following in my settings.py
my settings.py code snapshot
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'youtube',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sitemaps',
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Just for your information, I tried changing STATIC_ROOT to 'static' but didn't work.
STATIC_ROOT = 'static'
STATIC_URL = '/static/'
My directory structure:
my project dir structure
My HTML file from app youtube, and app directory structure
about page html file snapshot
<link rel="stylesheet" type="text/css" href="{% static 'youtube/about.css' %}">
My App.yaml
runtime: python
env: flex
runtime_config:
python_version: 3.7
entrypoint: gunicorn -b :$PORT yt_analytics.wsgi
automatic_scaling:
min_num_instances: 1
max_num_instances: 2
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
I have run below command to test on local server and then deploy on Google app engine.
python manage.py runserver
python manage.py collectstatic
gcloud app deploy
When I run collectstatic command I see it is creating new folder named static inside my project root directory.. i.e. yt_analytics/static
Now I see I have two static directories in my project--
yt_analytics/static
yt_analytics/youtube/static
When I ran findstatic command I got:
python manage.py findstatic youtube/about.css
Output:
Found 'youtube/about.css' here:
/home/work/Mirror/Projects/django/ytwebsite/yt_analytics/youtube/static/youtube/about.css
Have exhausted all forums and answers before asking this question. I am unable to deploy my app on GAE. Your help is appreciated.
Adding below code in urls.py after #Ivan Starostin's comment and it worked for me.
from django.conf.urls import url, include
from django.views.static import serve
urlpatterns = [
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Thanks Ivan for your valuable comment.
I'm using Django + Nginx + uwsgi
Settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
]
if DEBUG:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
else:
STATIC_ROOT = "/var/www/mysite/static/"
STATIC_URL = '/static/'
if DEBUG:
MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
else:
MEDIA_ROOT = "/var/www/mysite/media/"
MEDIA_URL = '/media/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
After update a code (in DEBUG = True all works good, static files are loading correct) I'm using python manage.py collectstatic on DEBUG = False in production server and it's collect admin static, but it's not collecting static for "main" app.
In debug mode all working good.
Checked serving by Nginx - it's working correct.
Why "collectstatic" doesn't collect static from "main" app?
UPD:
I'm an idiot. Forgot about add directory with static files in git. Sry.
I've got a project that runs on Heroku from a Dockerfile and heroku.yml.
The site generally works, but I am having trouble with static files.
collectstatic is run when building the pack.
If I set DEBUG to True, it finds the files.
I'm trying to use whitenoise but not sure why it's not working. It sounds so simple so I'm sure it's something silly.
heroku.yml
setup:
addons:
- plan: heroku-postgresql
build:
docker:
web: Dockerfile
release:
image: web
command:
- python manage.py collectstatic --noinput
run:
web: gunicorn records_project.wsgi
settings.py
MIDDLEWARE = [
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
'django.contrib.sites.middleware.CurrentSiteMiddleware',
]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.sites',
... more stuff here...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# for referencing files with a URL
STATIC_URL = '/static/'
# where to find static files when local
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
# location of satatic files for production
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# how Django should look for static file directories; below is default
STATICFILES_FINDERS = [
# defaults
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
# This gives me a 500 error
# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
urls.py
urlpatterns here...
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Any chance you are referencing a static file (e.g., a CSS file or image) in your template that doesn't exist? I was having this same problem because I had the following in my base template:
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
But no styles.css file in my css folder.
I had similar issue because static files, collected on release stage, were missing on run. So i changed my code to:
heroku.yml
setup:
addons:
- plan: heroku-postgresql
build:
docker:
web: Dockerfile
release:
image: web
command:
- chmod u+x entrypoint_heroku.sh
run:
web: ./entrypoint_heroku.sh
Dockerfile
FROM python:3.7
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update
RUN apt install -y netcat
RUN pip install pipenv
COPY Pipfile* /usr/src/app/
RUN pipenv install --system --dev
COPY . /usr/src/app/
RUN mkdir -p /storage/static/
entrypoint_heroku.sh
#!/bin/sh
python manage.py migrate --noinput
python manage.py collectstatic --noinput
gunicorn app.wsgi
settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = '/storage/'
STATIC_ROOT = MEDIA_ROOT + 'static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Not the best solution. But it helped me to run server with Debug=False
For what it's worth, I never found a way to get WhiteNoise to serve those static files. I swear it's worked in the past with a similar set up, so that will remain a mystery.
I received a tip from Matt from justdjango.com that Heroku doesn't want to serve static files from that same server. Once I moved my static files over to an AWS S3 bucket, all was well.
I also faced this issue. Although successfully collecting static files on release stage, they weren't present when dyno was up (thus Django missing manifest errors).
This setup worked:
heroku.yml
build:
docker:
web: Dockerfile
run:
web: ./heroku_entrypoint.sh
Dockerfile
--- some code ---
RUN chmod +x heroku_entrypoint.sh
RUN mkdir -p /app/static/
--- other code ---
heroku_entrypoint.sh
python manage.py collectstatic --noinput
python manage.py migrate
gunicorn app.wsgi:application --bind 0.0.0.0:$PORT
I know this question has been asked many times but none of the solutions seems to work for me. I just started using django and I'm having issues trying to get the CSS for the admin panel to show when I use runserver.
When I'm in: localhost:8000/admin/ none of the CSS shows up
<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/dashboard.css" />
That is what is shown in the in the HTML of the admin page, which those URL's it links to ends up becoming http://localhost:8000/static/admin/css/base.css which doesn't link to anything.
In settings.py I have
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'codeapp',
'django.contrib.admin',
'django.contrib.admindocs',
)
STATIC_ROOT = '/Users/datboitom/Sites/Codebase/codebase/codeapp/static/'
STATIC_URL = '/static/'
I'm just not too sure what else to do. If can help me to fix this issue that would be great. I've ran python manage.py collectstatic and the static files are in /Users/datboitom/Sites/Codebase/codebase/codeapp/static/ folder. There just doesn't seem to be any link between these files and whats trying to be loaded. One is trying to load off the localhost url and not the path of where its located on the computer.
Just in case someone run into this error again. While in Debug=False mode, Django won't serve static files for you anymore. Your production server should take care of that. But, if you still want to test your app locally in debug=false you should run the devserver in insecure mode:
manage.py runserver --insecure
Taken from here: Why does DEBUG=False setting make my django Static Files Access fail?
What is your setting like your server?
According to the documentation, you should not write anything at STATIC_ROOT
Leave STATIC_ROOT blank and instead, then, put the path in STATICFILES_DIRS.
Hope this helps
I guess you are missing the following in your settings.py file
# 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',
)
Config ERROR
in setting.py, you should add 'django.contrib.staticfiles' in INSTALLED_APPS
the codes show:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
......
OK,try it.
I had this problem just recently after changing dev machines.
It turned out that my new machine wasn't set to run django apps in debug mode locally, so adding
DEBUG = True
To my dev machine's settings.py sorted it.