When viewing page the image is not loaded. The image is uploaded via Django-admin The part of HTML template loading image is:
Home.html
<img class="card-img-top" src="{{ product.image.url }}">
settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
The error shown in the terminal:
Not Found: /media/img/sandstone.png [00/Aug/0000 00:00:00] "GET /media/img/image.png HTTP/1.1" 404 2581
Most likely, django is not configured to serve user uploaded files. To serve them, add following to urls.py
file:
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
However, please not that it is suitable only for local development, not for production. Relevant docs:
Adding the following to the urls.py served the purpose:
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.conf import settings from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.