Media files not showing on Debug False

2019-09-17 01:32发布

问题:

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)

回答1:

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/



回答2:

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.