Forums

Can upload files into media directory, but media files not served (Not Found)

Driving me mad: Normal static files like css and images are served fine. User uploaded images do not show up at all, but when I upload them through my app they are saved within the appropriate directory on PA.

When accessing the same URL though via browser it doesn't work. Tried to work with AWS S3 but configuring this was even worse.

Any pointers please????

settings.py

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

The Static files section under the Web tab is properly configured I believe:

URL

/static/
/media/

DIRECTORY

/home/dude1983/surfapp/src/static_cdn/
/home/dude1983/surfapp/src/media/

Collectstatic command works fine.

Check in your browser that the media files are actually being retrieved from the URLs you expect them to be returned from and that the start of the URL matches the URL you've mapped in the static files settings.

Did this get resolved? Mine is set up exactly the same way and I am having the same trouble. The media files are not being retrieved where I expect them to. I'm sure I am missing a step.

Do you have a static file mapping to the directory that you're trying to serve?

I've set MEDIA_ROOT and MEDIA_URL, like so.

STATIC_URL = '/static/'
STATIC_ROOT = (os.path.join(BASE_DIR, "static"))

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

I do not have STATICFILES_DIRS set, but my static files are working just fine. It's the user uploaded media files that I can't seem to retrieve, even though my application is able to save them in the correct location.

EDIT: Never mind I found it. For the next goober:

Finally, set up a static files mapping to get our web servers to serve out your static files for you.

-Go to the Web tab on the PythonAnywhere dashboard
-Go to the Static Files section
-Enter the same URL as STATIC_URL in the url section (typically, /static/)
-Enter the path from STATIC_ROOT into the path section (the full path, including /home/username/etc)
-Then hit Reload and test your static file mapping by going to retrieve a known static file.

Eg, if you have a file at /home/myusername/myproject/static/css/base.css, go visit http://www.your-    domain.com/static/css/base.css

Found here: https://help.pythonanywhere.com/pages/DjangoStaticFiles/

Glad you figured it out! :)

my files on the media folder too are not showing up don't know why

Have you tried the debugging steps on this help page?

Hello Thanks, I have seen the problem. I added the URL: /media/ and Directory : /home/ugocode/portfolio-project/media. then I deleted my browser Catched and Viola it worked !

Thanks for your response anyways! I Love pythonAnywhere

Excellent, very glad you worked it out :-) Thanks for the kind words!

Hello! Please help :( I already read the whole post. And I've tried everything they've published. I'm still having the same problem. I can not access my files in / media / but I can load them correctly Thanks!

MEESAGE: Not Found. The requested URL /media/documents/BasE.xlsx was not found on this server.........

BUT... my browser does receive the file, and I can download it in "Developer mode"

I can see this message in the Mozilla Browser: The character encoding of the HTML document was not declared. The document will be displayed with "junk" text in some browser settings if the document contains characters external to the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

Could you give us an example URL of a media file that is not downloading correctly?

I'm also facing similar problem, my files are uploaded on the media folder but are not showing in website don't know why. It was working fine for two days then it stopped showing media files the next day

You need to serve the files in the media folder. See our help pages for documentation and troubleshooting tips for static files.

Was having this same issue but realized that you need to include the media path on the webapp dashboard similar to static files. Did that and it worked like a charm.

None of this is working for me. My static files show up but not media (dynamic). I have the url and root in settings and set a path for /media/ under /static/ in the web tab. I just don't understand whats causing the not found error.

Here's our documentation on how to debug issues with static files: http://help.pythonanywhere.com/pages/DebuggingStaticFiles/