I've been scouring all over StackOverflow searching for a solution, but none of them seems to be working. I can't get my static files for my Django web app to load when I deploy it to Heroku. It works fine in localhost:8000 though. This is only when I set DEBUG = False . It works fine when DEBUG = True. My static files basically consist of several images for the home page and 1 CSS file.
My main project directory is mynotes.
My app directories are mynotess and users
settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See http://ift.tt/2dCkgoW
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'w2!3r+pgqz6yhwi_+aw_!-yra7#h69z-n3-ni$gs2v+!!k^2$b'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['localhost']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap3',
'mynotess',
'users',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
]
ROOT_URLCONF = 'mynotes.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'mynotes/templates')], #originally empty
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mynotes.wsgi.application'
# Database
# http://ift.tt/2fwPq4D
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# http://ift.tt/2fdZ2xm
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# http://ift.tt/2fwMntg
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# http://ift.tt/2fe1yDE
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
LOGIN_URL = 'users/login'
BOOTSTRAP3 = {'include_jquery': True}
if os.getcwd() == '/app':
import dj_database_url
DATABASES = {
'default': dj_database_url.config(default='postgres://localhost')
}
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.normpath(os.path.join(BASE_DIR, 'staticfiles'))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
urls.py (from my main project directory - 'mynotes')
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.views.static import serve
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^users/', include('users.urls', namespace='users')),
url(r'', include('mynotess.urls', namespace= "mynotess")), #telling it to look into the urls.py in mynotess for more URLS!
]
if settings.DEBUG:
urlpatterns += [
url(r'^static/(?P<path>.*)$', serve, {
'document_root': settings.STATIC_ROOT,
}),
] #I copied (sort of) this from the Django documentations but it still doesn't work.
The static files (jpg, png, CSS) are contained within a directory called 'static' in my app directory (mynotess).
Any kind of help will be greatly appreciated. I've tried every solution there is on StackOverflow and none of it seems to work. This works fine when DEBUG is true and only falls apart when DEBUG is false. Even so, it still works on localhost. Am I forgetting to do something here?
Aucun commentaire:
Enregistrer un commentaire