Reputation: 1
I have deployed my django project on Heroku (a very basic project without database). The homepage is working but I have a Server Error (500) when I ask for a page with an imag from my Django project. When I ask a page with an image from internet, it is working fine. So my deduction is that the static files are not properly served but I don't find what it is wrong in my coding.
Heroku log :
2020-09-10T04:31:02.719831+00:00 app[web.1]: 10.63.145.103 - - [10/Sep/2020:04:31:02 +0000] "GET /ES-home HTTP/1.1" 500 145 "https://hamaktest.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
2020-09-10T04:31:02.720549+00:00 heroku[router]: at=info method=GET path="/ES-home" host=hamaktest.herokuapp.com request_id=f9cc73c5-b170-4853-9ca7-368255259a52 fwd="117.196.158.121" dyno=web.1 connect=0ms service=60ms status=500 bytes=410 protocol=https
In the Heroku build :
Installing collected packages: asgiref, pytz, sqlparse, Django, gunicorn, whitenoise
remote: Successfully installed Django-3.1.1 asgiref-3.2.10 gunicorn-20.0.4 pytz-2020.1 sqlparse-0.3.1 whitenoise-5.2.0
remote: -----> $ python manage.py collectstatic --noinput
remote: 204 static files copied to '/tmp/build_72d0a30f/staticfiles', 481 post-processed.
Any idea is welcome. Thank you.
Jytar
My settings.py file :
ALLOWED_HOSTS = ['.hamaktest.herokuapp.com', '127.0.0.1']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website'
]
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',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'hamak.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'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 = 'hamak.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
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',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'```
Upvotes: 0
Views: 1193
Reputation: 4288
For anyone else coming to this common issue.
Make sure your HTML is tidy. I had commented-out incorrect references to CSS files in my base template that caused this issue for me. Just removing those fixed the problem.
Upvotes: 0
Reputation: 1
Problem Solved.
Django accepts the backslash in the image code : src="{% static "images\picture01.png" %}"
however Heroku don't.
I changed to src="{% static "images/picture01.png" %}"
And both Django and Heroku work.
I used backslash "" as shown in a Django tutorial. So be careful because it costed me a lot of time to find the problem.
Upvotes: 0
Reputation: 173
When Django 3.1 came out, the way you referenced BASE_DIR changed. If you don't want to use the new version of Django, make sure you are adding the version you would like to use in your requirements. txt file. Anywhere in your settings.py file that has (os.path.join) would need to be replaced with the new syntax.
# Old Way
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# New Way
STATIC_ROOT = BASE_DIR / 'staticfiles'
However, if that is not the cause of your problem, I have found that by disabling static file collection during your push to heroku helps things out.
heroku config:set DISABLE_COLLECTSTATIC=1
After you have a successful build with staticfiles collection disabled, I go into the heroku CLI and run
heroku run python manage.py collectstatic
Upvotes: 1