2

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

enter image description here

And here is my urls.py configuration as well

enter image description here

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

enter image description here

enter image description here

enter image description here

Can someone please help me point out what i am doing wrong or why django can't find my media folder?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
gathagu
  • 195
  • 4
  • 14
  • Can you put the code in text? Images are hard to use. – internet_user Dec 23 '17 at 07:12
  • 2
    Possible duplicate of [Python Django media url not working after setting DEBUG = True](https://stackoverflow.com/questions/28478159/python-django-media-url-not-working-after-setting-debug-true) – Kent Shikama Dec 23 '17 at 07:15
  • The question is not a duplicate of the reference since my problem is the other way round. my site works in development but my uploaded images in the media folder are not found when `debug = False` – gathagu Dec 23 '17 at 07:27
  • The +static usage is only for debug/dev mode. I believe you can fix it setting STATIC_URL and doing collectstatic. But I am not close to my regular workspace to be sure of it. I will look later if you don't figure it out. – Samantha Atkins Dec 23 '17 at 08:41
  • i already did collectstatic and the normal css and images are loaded but the uploaded images in media folder aren't found – gathagu Dec 23 '17 at 09:05
  • Possible duplicate of [Django shows 404 error on media files when debug = False in production](https://stackoverflow.com/questions/47948586/django-shows-404-error-on-media-files-when-debug-false-in-production) – schrodingerscatcuriosity Dec 23 '17 at 16:55

6 Answers6

2

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

atn
  • 904
  • 9
  • 17
1

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:

How to serve static files on pythonanywhere.

Amaan
  • 69
  • 2
  • 10
1

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

serving-file-in-production

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.

Jesse slco
  • 11
  • 1
1

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}),
]
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
1

Remove STATICFILES_DIRS within settings.py and replace it with

STATIC_ROOT = BASE_DIR / 'static'

when debug = False

from django.urls import re_path
from django.views.static import serve

re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), 
Peter Rosemann
  • 505
  • 8
  • 20
0

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.

Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
Adi Ep
  • 509
  • 1
  • 5
  • 22
  • Thanks for your reply, The media files are loaded when `debug = True` but when i turn it to `false` the images cannot be found. in other words on development it works but in production it doesn't load the images – gathagu Dec 23 '17 at 08:56