Forums

Media files not found

I'm stuck with not loading media files for 2 days and I would appreciate any ideas because I have run out of them. Here is my code:

Settings.py (main folder)

MEDIA_ROOT = '/home/conspects/conspects/media/'
MEDIA_URL = '/media/'

In my webapp page in static files section everything coincides: URL Directory /media/ /home/conspects/conspects/media/

I have tried playing with adding / and deleting, all variants, reloading server everytime - and nothing

Urls.py (main folder)

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

Model.py (other folder)

 image = models.ImageField('image_name', max_length=254)

I render all images in my views.py as 'conspects' and here is my rendered template:

{% for conspect in conspects %}
    <a href="{{conspect.image.url}}">
      <img class="img" src="{{conspect.image.url}}"></img>
    </a>
{% endfor %}

Hope you'll see a mistake!

If you look at the Web developer console in your browser (in Chrome you'll find it under the "hamburger" menu, then "more tools", then "developer tools"), you can see what URLs your page is requesting -- so if you display that and then visit your page, that should help work out what it's requesting. Could you try that and see if that helps you work out what the problem is? If it doesn't clarify things, could you post one of the URLs that should be working and isn't?

The mistake I have in console is "GET http://conspects.pythonanywhere.com/media/my_image.png 404 (Not Found)". I think, that django just does not understand what to return from that url.

If that helps, here is my loaded image:

 <a href="/media/my_image.png">
     <img class="img" src="/media/my_image.png"></img>
 </a>

Do you have static files mapping from /media/ to /home/conspects/conspects/media/? Do you have my_image.png file in your /home/conspects/conspects/media/ directory?

I don't know, when I go to my route directory: '/home/conspects/conspects/' in files section, I do not see media folder, maybe I can check somehow else?

Your media directory will be wherever you have placed it. The first step to getting your files served is knowing where they are and, since that's a choice that you made, you will have to find them. We have a help page that covers how to debug serving static files (like media) here: https://help.pythonanywhere.com/pages/DebuggingStaticFiles/

Just to be sure, when I was developing my site, running it on local host, in my directory there was no media folder anywhere, but everything worked ok. However, as I understand there should such folder on pythonanywhere server, shouldn't it?

Could we look into your files? We can but we ask first.

Yes you may, I have changed my passwords, hope you'll find something

You have correct mapping, but the directory /home/conspects/conspects/media does not exist.

Ok, how can I fix that? Create media folder and add upload argument to the imagefield in models.py? Or maybe something else?

If you didn't have a media folder on your local machine, then you need to look at the settings that you have on your local machine for the MEDIA_ROOT. That is where you media files are on your local machine. Then you need that directory to also be somewhere on PythonAnywhere and use that directory location in the MEDIA_ROOT setting on PythonAnywhere.

I think, that I figured it out. I have made a folder 'media' at this path 'home/conspects/conspects/media/', and added one of my test images there, and everything worked nice, for that image. But for others still nothing. I think, that django do not understand where to save, or something like this. How could I fix that?

Nevermind, i figured it out, i wasn't using form.is_valid, now everything works. Thank you a lot for feedback!

Great, thanks for letting us know!

i also stuck on that point please any help for this PermissionError at / [Errno 13] Permission denied: '/media/decoded_image2.png' Request Method: GET Request URL: http://ocrapi1.pythonanywhere.com/?img=hjhygyugft56565rdfcgvnh43456789zxcvbnm,xcvbnm, Django Version: 3.1.6 Exception Type: PermissionError Exception Value:
[Errno 13] Permission denied: '/media/decoded_image2.png'

You do not have write access to /media. Use a directory under /home/ocrapi1 to store your media files, not /media. You can get more information about how to serve the files here

i guess its not "right" way, but i can upload my media files when copied to settings.url full url to the media folder: MEDIA_URL='https://www.pythonanywhere.com/user/AndreiEkb/files/home/AndreiEkb/open_stat_site/python_proj/python_proj/media/'

but, i dont' know safaly this or not, so i would like learn right way to do this

It's not a correct way -- the url you used works only if you're logged into PythonAnywhere as you (AndreiEkb). Have a look at this help page.