I've successfully deployed Django with Whitenoise on Cloudfoundry except for the compression. If I set on my settings.py to enable compression:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
I get a 500 error and nothing on my cf logs. Please not that I don't have SSH access nor/heroku as this is running on Predix.
My settings.py:
STATIC_URL = "/media/"
STATIC_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'itcave/media'),
]
MIDDLEWARE_CLASSES = [
'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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
wsgi.py:
from whitenoise.django import DjangoWhiteNoise
from django.core.wsgi import get_wsgi_application
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "itcave.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Please also note that all my static files are stored within a media folder at the same level as my settings.py file.
Adding web: python itcave/manage.py collectstatic --noinput on a line before my run command in Procfile didn't work. ALLOWED_HOSTS is correct because when DEBUG = True everything runs fine.
Related
I got Error500 when I turn DEBUG True on settings.py but everything is okay when DEBUG is False. python manage.py collectstatic is okay too.
Here my settings.py. I will appreciated your help. I use Django 2.2 version.
Django settings
"""
Django settings for samamarche project.
Generated by 'django-admin startproject' using Django 2.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import dj_database_url
import os
import django_heroku
if os.environ.get('ENV') == 'PRODUCTION':
# ...
db_from_env = dj_database_url.config(conn_max_age=500)
#DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
DATABASES['default'].update(db_from_env)
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = os.environ.get('SECRET_KEY', 'xxxxxxxxxxxxxxx')
DEBUG = True
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'phonenumber_field',
'marche',
'cart',
'commandes',
'payment',
'coupons',
#'accounts',
'crispy_forms',
'users',
]
MIDDLEWARE = [
'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'
]
i think adding ALLOWED_HOSTS = ['*'] will solve the problem
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 don't know if I am explaining my problem correctly but I can clarify with the following picture of my settings.py file
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
DEBUG = False
# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['sheryar-portfolio.herokuapp.com', '127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
'calculator',
'amino'
]
MIDDLEWARE = [
'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',
]
Now here in ALLOWED_HOSTS, I have declared two hosts.
The Heroku one is running perfectly whereas I am having some issues in the local one.
It seems like that my static folder is not being located by django as I can only see the HTML of my website in local server.
In short, I cannot see the CSS being applied to my website when I am including the local server in ALLOWED_HOST
I am using django-storages to serve media files on Dropbox. But I cannot get it working (media files are still stored in local server).
I've installed dropbox and django-storages, and created an app with permission type:app folder, then added related settings.
Here's my settings:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
DEBUG = False
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_filters',
'website',
'storages',
)
# for dropbox
DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = 'my_generated_token_from_dropbox'
DROPBOX_ROOT_PATH = 'media'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
To be able to upload the media files to Dropbox (and reading them) what has to be changed in the settings code?
Ok, found the problem! The settings are correct, the problem was that for some reason I was using a custom storage for my FileField! All I had to do was removing the storage=...:
file = models.FileField(upload_to=some_path, storage=CustomStorage())
to
file = models.FileField(upload_to=some_path)
I have already finished my website made with Django and would like to put it up. I found Heroku and saw that is free for my basic needs and would like to host it there. My site uses SQLite not PostgreSQL so I would like to know how to use that since it doesn't support sqlite. I found the getting started with Django page on Heroku but I didn't use pip or virtualenv to set it up.
What steps should I follow to get my site up? Just FYI I am using latest Django dev branch 1.8 and Python 3.4.1 and it is a personal website.
Here is my settings.py file
"""
Django settings for mysite project.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0r58%!j00w2q1faj*57=d)*fv^=ai#-wgnakj91^--z5f(ohq1'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
WSGI_APPLICATION = 'mysite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/dev/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "templates"),
)
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"),
)
You need a requirements.txt with all your dependencies, because heroku uses that file to provide you the services you need.
After you added the postgreSQL add-on to your heroku project, heroku generates you a DATABASE_URL environment variable. you can parse that in your settings.py according to heroku's tutorial:
# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] = dj_database_url.config()
the basic wsgi.py is also provided in the tutorial, you can just use that.
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
commit the application to your local git repository and push it to the heroku remote repository. if you do that on the console you'll get good feedback about your deployment.
basically you need to follow the getting started tutorial. skipping the virtual environment stuff just makes generating the requirements file more complicated, but it is possible.