每件事在调试模式下都可以正常运行,但是在调试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')
和项目网址
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
)
尽管使用模板标记可以完美显示网址链接路径
{% load staticfiles %}
<a href="{{ magazine.document.url }}" target="_blank">
<i class='far fa-file-pdf'></i>
</a>
希望您可以帮助解决此问题。