Forums

Images not displaying when I switch to Debug=False

I switched to Debug=False in my Django project and set the ALLOWED_HOSTS to 'mccall114.pythonanywhere.com'. When I load my page, none of the images are displayed (they are within the media subfolder). The CSS from my static folder displays fine.

I would appreciate any insight into the issue here. Error log is below.

2014-10-03 22:57:00,396 :Internal Server Error: /art/ Traceback (most recent call last): File "/home/mccall114/.virtualenvs/rango/lib/python2.7/site-packages/django/core/handlers/base.py", line 92, in get_response response = middleware_method(request) File "/home/mccall114/.virtualenvs/rango/lib/python2.7/site-packages/django/middleware/common.py", line 57, in process_request host = request.get_host() File "/home/mccall114/.virtualenvs/rango/lib/python2.7/site-packages/django/http/request.py", line 72, in get_host "Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): %s" % host) SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): mccall114.pythonanywhere.com

Do you need to set up a static files mapping for the MEDIA_URL / MEDIA_ROOT, as well as the STATIC_URL/STATIC_ROOT settings?

Do you mean within my settings.py file or in pythonanywhere's Web tab?

I just added to the Web tab a mapping from /media/ to /home/mccall114/art/media, but it still isn't working.

I'm not sure what I changed but it appears to be working now. Thanks for your help.

:-)

same is the case with me.... my css and all static files are working fine... but images are not showing i have set the media root is well.

plz help.

We have a guide here, maybe it helps? http://help.pythonanywhere.com/pages/DjangoStaticFiles

Thank's harry for the answer.

actually now everything is working images are coming through but its just because i turned debugging to True if i turned debugging to False then again images are not working???

can you help me out....!

Did you follow the guide here? http://help.pythonanywhere.com/pages/DjangoStaticFiles ?

Hello,

I had the same problem but the solution above worked for me.

In your web tab, you need to map /media/ to /home/yourusername/yourprojectname/media/

Hope this helps :)

I changed my MEDIA_ROOT from os.path.join(BASE_DIR, "media") to BASE_DIR. And my MEDIA_URL to '/'. I changed mecause i got exception ' "/home/noveo/imageboard/mysite/media/1.jpg" does not exist '. Also, when i click 'Reload username.pythonanywhere.com', appears sign 'Your webapp took a long time to reload. It probably reloaded, but we were unable to check it.'. But it worked. A little time later it stopped to work. A little time later it started work again

Hello,

I had the same problem but finally, I solved it.

Here's the solution,

in settings: DEBUG = False when we set debug false it will create a security lock on that' setting.py file folder, that's why if our media folder inside that folder, our server can access the media folder and images, if we set this DEBUG = True then security will off and we can access it, it's important to set the DEBUG = False during the deployment or our project on hosting.

Cut your media folder from setting.py folder and paste the media folder outside of setting.py file folder means paste it there with other files like manage.py or database etc,

in your setting.py file configure like that

MEDIA_URL = '/media/'

MEDIA_ROOT = '/home/dawer09/blog_project/media/'

and also set the media root on your web app page ('dawer09.pythonanywhere.com"')

dawer09.pythonanywhere.com

thanks for letting everyone else know!

In your webapp page, go to the Static files setting and add your directories where you have images. like this url directory /static/ /home/path; /multimedia/ /home/path/multimedia; /photos/ /home/path/photos;

multimedia, photos, .. these directory must be in your project directory near to manage.py

In your webapp page, go to the Static files setting and add your directories where you have images. like this

URL Directory /static/ /home/path/staticfiles; /multimedia/ /home/path/multimedia; /photos/ /home/path/photos;

my configuration

Are you having problems with your site? All of the images seemed to be loading when I checked it just now.

Same problem.. just make sure map static files folder and media files folder on the mapping "web" tab..

It's probably worth linking again to our help page on how to set up static files in Django in this topic, as the previous links are quite far up in the discussion.

One Solution I found for the media files & the static files load in production mode or when DEBUG=FALSE

First Add below code or Import above of urls.py file.

from django.views.static import serve

Then, Add the Lines below of your URL Patterns,

if not settings.DEBUG: urlpatterns += [ url(r'^uploads/(?P<path>.)$', serve,{'document_root': settings.MEDIA_ROOT}), url(r'^static/(?P<path>.)$', serve,{'document_root': settings.STATIC_ROOT}), ]

Hope this will work for you. It is worked for me.

For more check this link:

https://www.stackofcodes.in/2019/06/05/django-media-files-not-loading-when-debug-false/

One Solution I found for the media files & the static files load in production mode or when DEBUG=FALSE

First Add below code or Import above of urls.py file.

from django.views.static import serve

Then, Add the Lines below of your URL Patterns,

if not settings.DEBUG: urlpatterns += [ url(r'^uploads/(?P<path>.)$', serve,{'document_root': settings.MEDIA_ROOT}), url(r'^static/(?P<path>.)$', serve,{'document_root': settings.STATIC_ROOT}), ]

Hope this will work for you. It is worked for me.

For more check this link:

https://www.stackofcodes.in/2019/06/05/django-media-files-not-loading-when-debug-false/

This solved it for me. Many thanks.

Just to be clear the above will work, but it's not a great solution. It will not use the static files handling that PythonAnywhere provides which means it cannot take advantage of the optimisations that we provide for static files. There is extensive help on our help pages for how to configure and debug static files.

thanks everyone, it works for me.

Excellent, thanks for confirming!

Hello everyone

even i had same issue thanks to marilynmags!!!

just did the below modifications and it worked In your web tab, you need to map /media/ to /home/yourusername/yourprojectname/media/