0

I am trying to serve dynamically created files on my Django server. The code works fine and serves the image file from media folder on my local server. It also works and serves the image file in Production server but only when I set debug to True in Prod server.

But when I set debug to False in Prod Server, I get a file not found error (in console): GET http://thesitename/media/imgs/img.png 404 (Not Found)

The server serves the files from the /static folder, but does not serve the files from /media folder.

my settings.py looks like this:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "frontend/media/")

urls.py looks like this:

app_name = 'frontend'
urlpatterns = [ ###urls###
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Views.py looks like this:

def template_page(request, loation, par, date):
    ### generating image using pil here ###
    pil_image.save("frontend/media/imgs/img.png”)
    path_to_image_passing_to_template =  “/media/imgs/img.png”
    params = {
        'path_to_image_passing_to_template' : path_to_image_passing_to_template
    }
    return render(request, 'frontend/template_page.html', params)

html looks like this:

<img src="{{path_to_image_passing_to_template}}" />

and file structure is like this:

/backend 
    urls.py
    views.py
/frontend 
    /static
    /media
        /imgs
    /templates
        /frontend
        urls.py
        views.py
/thesite
    settings.py
    urls.py
    wsgi.py
/static

I don't understand why it should work with debugging turned on in prod server and not otherwise!!!

I have looked at several questions but did not find any query close to this kind of issue. I took some help while writing working out the dynamic media files code from this answer earlier but am stuck right now.

  • 1
    Possible duplicate of [Why does DEBUG=False setting make my django Static Files Access fail?](https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail) – 9000 May 04 '18 at 18:54
  • My website is able to serve files from static folder (images and text files) but is not serving files from media folder which I added few days ago – Ritambhara Chauhan May 04 '18 at 18:58
  • 1
    You need to configure Apache to server your `media` directory, just as you have done for the `static` directory. – Alasdair May 04 '18 at 22:12
  • Thanks Alasdair! I am still looking on how to configure Apache server. is there any documentation I could refer to for this? (I did not do it for static directory, and was given already setup and functioning server) – Ritambhara Chauhan May 05 '18 at 03:49

1 Answers1

0

Confirm that you have 'os.environ' module loaded...try executing the code below on the production server.

import os
print(os.environ.get('MEDIA_ROOT'))

Make sure it shows the expected path.

then ensure that your 'urls.py' and 'Views.py' have 'import os' line appearing at the top of the file. Good Luck!

webmite
  • 575
  • 3
  • 6