DEBUG=False Django 2.2.6 中未显示的媒体文件

Media Files not shown in DEBUG=False Django 2.2.6

在调试模式下一切正常,但在调试 id false 时无法在生产环境中显示媒体文件

这是我的设置

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')

和项目 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
                        )

尽管 url link 路径使用模板标签完美显示

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

希望您能帮助解决这个问题。

这是我的错 PDF 文件必须由第三方查看器下载或查看。对于调试模式,当调试为 False 时,我必须让 Apache 服务器工作...希望这可以帮助任何遇到同样问题的人