Django IMAGE path error

When utilizing the localhost for a Django project, the form submission with an attached image correctly directs the image to the specified path (e.g., media/images). However, upon deploying the website and submitting a form, only the text is displayed, and the image cannot be found at the designated path.

THE ERROR
media\qrs\qr_ZQRQLNXGPA6ILUKVK9XE.png” does not exist

“”"
Django settings for TransitSynch project.

Generated by ‘django-admin startproject’ using Django 4.2.5.

from pathlib import Path
import os

BASE_DIR = Path(file).resolve().parent.parent

SECRET_KEY = ‘mykey’

DEBUG = True

ALLOWED_HOSTS = [‘*’]

INSTALLED_APPS = [
“captcha”,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘Website’,
‘users’,
‘crispy_forms’,
‘crispy_bootstrap4’,
‘phonenumber_field’,

]
AUTH_USER_MODEL= ‘users.CustomUser’

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 = ‘TransitSynch.urls’

TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: ,
‘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 = ‘TransitSynch.wsgi.application’

import dj_database_url
DATABASES = {
‘default’: dj_database_url.parse(‘postgres://mydatabase’)
}

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 = ‘Asia/Manila’

USE_I18N = True

USE_TZ = True

STATIC_URL = ‘/static/’

if not DEBUG:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

CRISPY_ALLOWED_TEMPLATE_PACKS = “bootstrap4”

CRISPY_TEMPLATE_PACK = “bootstrap4”

LOGIN_REDIRECT_URL = ‘/’

AUTHENTICATION_BACKENDS = [‘users.backends.EmailBackend’]

DEFAULT_AUTO_FIELD = ‘django.db.models.BigAutoField’

MEDIA_ROOT = os.path.join(BASE_DIR, ‘media’)
MEDIA_URL = ‘/media/’

EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’
EMAIL_HOST = ‘’
EMAIL_FROM = ‘’
EMAIL_HOST_USER = ‘’
EMAIL_HOST_PASSWORD = ‘’
EMAIL_PORT = 587
EMAIL_USE_TLS = True

MEDIA_URL = ‘/media/’

Hi there,

Have you checked out our docs on setting up static files with Django? https://render.com/docs/deploy-django#static-files

Let us know if you have any further questions.

Regards,
Mike


Render Support Engineer, MT (UTC-7)

hi mike,

i applied the changes in my settings

import dj_database_url
DATABASES = {
‘default’: dj_database_url.parse(‘postgres://transitsync_user:LW1f9iVJ6kw46SWefOFm75o5PcE1wfd9@dpg-clhfill8td7s73bo8cfg-a.singapore-postgres.render.com/transitsync’)
}

This is my databases

STATIC_URL = ‘static/’
STATICFILES_DIRS = [os.path.join(BASE_DIR, ‘static/’)]
STATIC_ROOT = os.path.join(BASE_DIR, ‘staticfiles’)

if not DEBUG:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

and i also have the middleware whitenoise but i still cant receive the image in my media. the external databases not reflecting the image that i submit in my forms

Hi Andrew,

Just to clarify, is the Django site you are working on hosted by Render?

Regarding the static file configuration, another thing to try that has helped others is, in the settings.py file, modifying this key:

STATICFILES_STORAGE = 'whitenoise.storage.StaticFilesStorage'

Regards,
Mike


Render Support Engineer, MT (UTC-7)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.