Search code examples
djangocpanel

Media Files not shown in DEBUG=False Django 2.2.6


Every thing works perfect in debugging mode but can't show the media files on production environment while debugging id false

That's my settings

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR,],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
                'django.template.context_processors.debug',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.csrf',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.static',
                'django.template.context_processors.media',
            ],
        },
    },
]

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.core.context_processors.debug",
    "django.core.context_processors.auth",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
)

STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticroot')

# Media folder for database media
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

and the project urls

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

Although the url link path shows perfectly using template tags

{% load staticfiles %}
<a href="{{ magazine.document.url }}" target="_blank">
      <i class='far fa-file-pdf'></i>
 </a>

Hope you can help to fix this issue.


Solution

  • It was my fault PDF files must be downloaded or viewed by third party viewer.. and for debugging mode I must have Apache server to work when debugging is False.