Media files not showing on Debug False

2019-09-17 00:46发布

I've just deployed a site with pythonanywhere, when I set to DEBUG mode to False, my media images dissapear, the path of my media folder is not found. I was asking myself what causes this issue and how I can resolve it ?

Here is how I configured my settings :

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

urls.py

urlpatterns = [
    ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

2条回答
别忘想泡老子
2楼-- · 2019-09-17 01:17

The behaviour you are seeing is by design, Django doesn't serve static files in production mode. Serving many, potentially huge static files using Python will put a lot of stress on the server, while any of the common servers will handle that with ease.

Hence you need to serve them using Apache or nginx yourself: https://docs.djangoproject.com/en/1.10/howto/static-files/deployment/

查看更多
smile是对你的礼貌
3楼-- · 2019-09-17 01:36

Found it, with pythonanywhere I had to setup media on the static files handler like this :

path : /media/

directory : /home/<myusername>/<myproject>/media

Then reload the domain name after setting DEBUG to False.

查看更多
登录 后发表回答