0

I have some product pictures uploaded in media folder that displays fine in development server, but after deployment I receive the 404 ressource not found error.

In settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'node_modules')]

STATIC_ROOT = os.path.join(BASE_DIR, 'productionStatic')

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'

In urls.py

from django.contrib import admin
from django.urls import path
from index import views
from index.views import indexPage, hdnytorv
from webshopRestaurant.views import hd2900_webshop_Main, AddressCheckForDeliverability, ChangeItemQuantity
from webshopCustomer.views import TakeawayCheckout, totalPriceDeliveryPossible, DeliveryForm, PickUpForm, localDeliveryCheckoutAddressCheck, Payment, PaymentComplete
from django.conf import settings
from django.conf.urls.static import static

admin.autodiscover()

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', indexPage.as_view(), name = 'index'),
    path('hdnytorv', hdnytorv.as_view(), name='hdnytorv'),
    path('hd2900', views.hd2900.as_view(), name='hd2900'),
    path('hd2900_takeaway_webshop', hd2900_webshop_Main.as_view(), name="hd2900_takeaway_webshop"),
    path('check-address-for-deliverable', AddressCheckForDeliverability.as_view()),
    path('changeItemQuantityInBasket', ChangeItemQuantity.as_view()),
    path('isPriceAboveDeliveryLimit', totalPriceDeliveryPossible.as_view()),
    path('hdbynight', views.hdbynight.as_view(), name='hdbynight'),
    path('takeawayCheckout', TakeawayCheckout.as_view()),
    path('deliveryFormCheckout', DeliveryForm.as_view()),
    path('pickupFormCheckout', PickUpForm.as_view()),
    path('local_delivery_checkout_is_address_deliverable', localDeliveryCheckoutAddressCheck.as_view()),
    path('localDeliveryPayment', Payment.as_view()),
    path('paymentComplete', PaymentComplete.as_view()), 
] 

#When in production medida url must always be added to urlpatterns
#if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

To display the product in view I have in my html template

<img class="card-img embed-responsive-item" src="{% get_media_prefix %}{{ item.product.image_path }}" alt="{{item.meta_description}}">

The pictures are uploaded to this location django_project_folder/media/productImages. I have assured that it is owned by www-data group and also media and all subfolders have 775 access rights. In development server the images shows up all right but in production I got 404 not found error.

jxw
  • 586
  • 2
  • 6
  • 36

1 Answers1

0

This issue is a duplicate. See the answer here I fixed it by adding in urls.py

from django.urls.conf import re_path
from django.views.static import serve

re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
jxw
  • 586
  • 2
  • 6
  • 36