'Django cannot find my media folder while debug = False
I have a django project that works very well and shows all media files uploaded from the admin when debug = True
but immediately i turn change to debug = False
django cannot find my media folder yet it loads my static folder. as you can see i have set up my MEDIA_ROOT
and MEDIA_URL
correctly
And here is my urls.py
configuration as well
And in the console logs i find my images output 404 errors while i can clearly see a media folder is present with the images in my directories
Can someone please help me point out what i am doing wrong or why django can't find my media folder?
Solution 1:[1]
Django does not processes static files in production mode, you need to call ./manage.py collectstatic
and serve collected static files with web server on the front of your django app.
Or run you dev server with --insecure
option. https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#cmdoption-runserver-insecure
Solution 2:[2]
You can check this answer Media files not showing on Debug False (" 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.")
Otherwise, If I'm right than you want to host your website on a server. Once you turn debug = True to False, Django no longer handle the static files by itself. Now, it's the server which handles the static files. I have this problem when I uploaded my django project to pythonanywhere. In order to solve the problem, you have to explicitly tell the server where your static files are located:
Solution 3:[3]
The problem is you are not ready to deploy your website, if not true, you should find a way to server your web first, not simply running manage.py runserver, it's just not practical.
One good way to deploy your django website is use Apache2 and mod_wsgi, in this case, you need to make some configuration with apache server's httpd.conf file, and apache2 will take care of serving files(static,media).
the httpd.conf will look likes this, also make sure to check django doc.
serving-uploaded-files-in-development
Alias /robots.txt /path/to/mysite.com/static/robots.txt
Alias /favicon.ico /path/to/mysite.com/static/favicon.ico
Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/
<Directory /path/to/mysite.com/static>
Require all granted
</Directory>
<Directory /path/to/mysite.com/media>
Require all granted
</Directory>
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
if you are not ready to deploy your website, you should not turn debug to false.
Solution 4:[4]
Adding this code below to "urls.py" displays media files in "DEBUG = False":
# "urls.py"
from django.conf.urls import url
from django.views.static import serve
from django.conf import settings
urlpatterns = [
# ...
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
]
Solution 5:[5]
pay attention all of your pages need to include:
{% load static %}
you have to configure Apache to serve the media files.
You shouldn't use collectstatic for your media dir. Remove '/home/admin/webapps/mainfolder/mainapp/media' from STATICFILES_DIRS, then set
MEDIA_ROOT = '/home/admin/webapps/mainfolder/mainapp/media'
Once you've done this, the static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) should serve media files when DEBUG = True
.
For DEBUG = False
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | atn |
Solution 2 | |
Solution 3 | Jesse slco |
Solution 4 | Kai - Kazuya Ito |
Solution 5 | Samsul Islam |