当debug = false时,Django媒体文件不起作用 [英] Django media files doesn't work when debug=false

查看:242
本文介绍了当debug = false时,Django媒体文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

settings.py

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


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.

推荐答案

静态帮助函数是在DEBUG = False模式下不起作用。而且不应该。在产品中,使用Django服务静态/媒体文件不是推荐。配置您的网络服务器( Nginx

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.

错误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.

这篇关于当debug = false时,Django媒体文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆