'Django - images uploaded by user does not display and shows 404 when Debug = False

I am developing a Django and using ImageFiled attributes into models that I need to display later. When I run the website in dev (DEBUG = True) it works, but when I change it to False (Production) uplaoded images do not display anymore and the console shows:

"GET HTTP/1.1" 200

As I saw in other open questionsI tried to add in settings:

MEDIA_ROOT = os.path.join(BASE_DIR, 'images') 

and in urls

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

But this doesn't work and even creates the same issue as in production.

settings.py:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home.apps.HomeConfig',
'gallery.apps.GalleryConfig',
'storages',
]


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',
'django.middleware.locale.LocaleMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
 ]

 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',
        ],
    },
},
]

DEBUG = False
if not DEBUG:
     STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
     STATICFILES_DIRS = (
         os.path.join(BASE_DIR, 'static'),
     )

     STATIC_URL = 'static/'
     STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
     db_from_env = dj_database_url.config()
     DATABASES['default'].update(db_from_env)

Update:

The documentation says:


URL that handles the media served from MEDIA_ROOT, used for managing stored files. It must end in a slash if set to a non-empty value. You will need to configure these files to be served in both development and production environments.

If you want to use {{ MEDIA_URL }} in your templates, add 'django.template.context_processors.media' in the 'context_processors' option of TEMPLATES. https://docs.djangoproject.com/en/1.8/ref/settings/#media-root----


I tried this and the problem is still there.



Solution 1:[1]

I normally set these in the urls.py for the project to handle media and static files correctly.

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT).

I think you are missing the settings.DEBUG if statement in urls.py config.

Solution 2:[2]

The problem has been solved by using AWS storage instead of the server storage. Effectively, as been said in the answers Django is not optimised to server static/media.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Wael