Django 媒体文件在 debug=false 时不起作用 [英] Django media files doesn't work when debug=false
问题描述
settings.py
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'staticfiles')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'data')
MEDIA_URL = '/data/'
<小时>
urls.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
handler404 = 'generic.views.invalid_request'
当我设置 DEBUG=False 并使用python manage.py runserver --insecure
所有静态文件都成功提供,但媒体文件没有出现.在调试控制台媒体 URL 引发错误 500.
When I set DEBUG=False and run the server using
python manage.py runserver --insecure
all static file are serve successfully but media files doesn't appear. In debug console media urls raise error 500.
推荐答案
static
辅助函数 在 DEBUG=False 模式下不起作用.而且不应该.在 prod 中使用 Django 提供静态/媒体文件 不是推荐.配置您的网络服务器(Nginx、Apache,..) 来提供这些文件.
static
helper function does not work in DEBUG=False mode. And should not. Serving static/media files with Django in prod is not recommended. Configure your webserver (Nginx, Apache,..) to serve these files.
error 500
- 调查日志文件以了解导致应用失败的原因.静态文件服务成功
可能它们是从浏览器缓存中获取的.
error 500
- investigate log files to understand what causes app failure.
static file are serve successfully
perhaps they are being taken from browsers cache.
这篇关于Django 媒体文件在 debug=false 时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!