T
T
thelaim2019-11-04 12:32:00
Django
thelaim, 2019-11-04 12:32:00

Pictures are not displayed with Debug=False?

My site is hosted on PythonAnywhere servers, but images for posts I create in the admin are not loaded. At the same time, the files with pictures themselves are located in a folder on the server. I tried all the solutions I found, but they did not lead to the desired results. One of them was taken from the documentation, I registered the STATIC_ROOT and STATIC_URL paths, and registered /manage.py collectstatic in the console. I have wsgi on the server.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
thelaim, 2019-11-05
@thelaim

I found a solution, I had to configure the display of media files on the toolbar, like this:
Go to the Web tab on the PythonAnywhere toolbar.
Go to the "Static files" section
Enter the same URL as MEDIA_URL in the URL section ( / media / )
Enter the path from MEDIA_ROOT in the path section (full path including / home / username / etc)
Then click "Reload", and uploaded files will be served correctly.

N
nasausa, 2019-11-04
@nasausa

project urls.py

from django.urls import path, re_path
from django.conf.urls.static import static
from django.conf import settings
from django.views.static import serve

urlpatterns = [
    ...
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT,}),]

Y
Yura Khlyan, 2019-11-04
@MAGistr_MTM

See if there are media settings

MEDIA_ROOT = location('media')
MEDIA_URL = '/media/'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question