Forums

django rest framework media files not found

Hi,

I am a beginner. I have an API that serves the client with news posts. Each post has an image that is stored in the media folder. As I know, and it also worked a few days ago - the MEDIA_ROOT and MEDIA_URL should be configured in the settings file. This is what I did and it works well on localhost and not in production (404 not found).

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

If someone can help figure this out I will be very happy :D. If you need me to provide more info about project configuration you are more than welcome to ask questions.

You need to serve the files on PythonAnyhwhere. see https://help.pythonanywhere.com/pages/DjangoStaticFiles/ and https://help.pythonanywhere.com/pages/DebuggingStaticFiles/

But isn't the media that serves the images and static files for CSS, javascript, and others? And, should I run collectstatic after a user has posted a new image?

Django splits static files into static and media files. collectstatic is only for static files, not media files. You still need to serve your media files using our static files mapping system.