Media files in production

hello to all,

I’m trying to make a feature that allows users to change their profile picture but when I put it in production the images don’t exist.

Is there any way to serve media files with django in production?

my configuration in settings.py is:

MEDIA_URL = '/ media /'
MEDIA_ROOT = str (BASE_DIR.joinpath ('media'))

and in urls.py:

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

Three things to check:

  1. Does the file exist where you are expecting it to be?

  2. Can you retrieve that file directly by entering the URL for it in your browser?

  3. Is the page rendering the right URL for that image?

  1. in local I have a file in the main directory “media/profiles” where all the images are stored, but in production when I download the code that folder does not exist.

  2. no, I get a 404 error (not found)

  3. when debug=True it returns the url, when debug=False it returns nothing.

How are you running the server in production?

with the free version of heroku

Django does not support serving media files with DEBUG set to False. Heroku’s documentation suggests using Whitenoise: Django and Static Assets | Heroku Dev Center.

-Jorge

Heroku does not preserve user-uploaded media files between dyno restarts. I usually go straight to AWS s3 and is pretty easy to set up. Heroku also has data stores that can be used, but I have yet to try them. Cloudinary is also an option.

1 Like

on this same question, linux ubuntu 20.0, i can access my path to http://ip:8000/media/default.jpg,

but when i register through the form i get
[Errno 2] No such file or directory: ‘/media/default.jpg’

please any help