0

I Build a WebApp and I am stuck on a Problem.

Images are not showing in Production (DEBUG = False)

settings.py

MEDIA_ROOT= '/home/new/gitrepo/main/media'
MEDIA_URL = '/media/'

STATIC_URL = '/static/'

STATIC_ROOT = '/home/new/gitrepo/static/'

What have i tried

I tried python manage.py collecstatic BUT images are still not showing in the Website.

I found many answers on this Topic like THIS. BUT it didn't worked for me.

I don't know what i am doing wrong.

Any help would be appreciated.

Thank You in Advance.

Lars
  • 1,234
  • 1
  • 8
  • 31
  • That is the expected behavior. Django does *not* serve static files in production. You need to configure Apache/Nginx/... to serve static files. – Willem Van Onsem Mar 01 '21 at 11:58
  • `collectstatic` only collects the static files in a directory. – Willem Van Onsem Mar 01 '21 at 11:58
  • BUT before days ago it worked and NOW it i a stuck on that problem – Lars Mar 01 '21 at 11:59
  • then likely this is due to caching, but as you can see (in the green box), Django deliberately does not do that for performance and security reasons https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files – Willem Van Onsem Mar 01 '21 at 12:00
  • AND i also don't know , How can i do Apache/Nginx, I've never worked with that – Lars Mar 01 '21 at 12:01
  • well that is the webserver that normally does the port forwarding to the Django application. Django runs on port 8000, so the webserver will forward incoming calls on port 80. – Willem Van Onsem Mar 01 '21 at 12:03
  • the configuration is explained In the linked documentation. – Willem Van Onsem Mar 01 '21 at 12:03
  • My `static files` are showing perfectly BUT only images are not displaying – Lars Mar 01 '21 at 12:05
  • @Progam check this post to serve media files on Nginx https://www.digitalocean.com/community/questions/how-to-serve-media-files – Ankit Tiwari Mar 01 '21 at 12:07
  • @Progam: that is likely because a browser will *cache* style files, javascript files, etc. – Willem Van Onsem Mar 01 '21 at 12:07
  • How does your Pythonanywhere static files mapping look like? Take a look at PythonAnywhere help page about Django static files https://help.pythonanywhere.com/pages/DjangoStaticFiles – Filip Mar 02 '21 at 09:55
  • @Filip Static file loading correctly BUT media and images are not loading – Lars Mar 02 '21 at 14:47

1 Answers1

2

Was it working in development? I think yes

open Pythonanywhere.com => choose "Web" from navbar => scroll to static files

and here you let pythonanywhere know where are the URLs for showing static data from your server

Add those two

URL                 Directory                     Delete

/static/            /home/myWebsite/static
/media/             /home/myWebsite/media

The /home/myWebsite/media is a path to where you store the static files for the given URL

here is a screenshot of my configurations My configurations

If it's not showing make sure you have something in your main URLS.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Zeyad Shaban
  • 896
  • 1
  • 8
  • 23