0

Page not found (404) “/app/media/products/23/05/01/jeans.jpg” does not exist while delpolying on railway

enter image description here (as i have turned debug on for the exact error) .

i get this error when i am trying to open my image in new tab,it is not showing after deployment on railway,i have uploaded image after deploying on https://web-production-08528.up.railway.app/admin/ it is working fine when i am using localhost

settings.py

DEBUG =True
#DEBUG = config('DJANGO_DEBUG', '') != 'False'

ALLOWED_HOSTS = ['web','127.0.0.1:8000']


# Application definition

INSTALLED_APPS = [
    'shop',
    'cart',
    'orders',
    'payment',
    'phone_field',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'myshop.urls'
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS=[
    BASE_DIR.joinpath('static')
]



DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

CART_SESSION_ID='cart'


STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' 

urls.py

from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
admin.site.site_header = 'Admin Panel'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('cart/',include('cart.urls',namespace="cart")),
    path('orders/',include('orders.urls',namespace='orders')),
    path('payment/',include('payment.urls',namespace='payment')),
    path('',include('shop.urls',namespace="shop")),
    
]

if settings.DEBUG:
 urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

template

        <img src="{{product.image.url}}" alt="" height="500px" width="300px">
        <form action="{% url 'cart:cart_add' product.id %}" method='post'>
            {{cart_add_product_form}}
            <input type="submit" value='Add to Cart' id="submit">
            {% csrf_token %}
        </form>
    </div> 
shafquet
  • 19
  • 4
  • Show how you refer these images in a template. And please clarify: is this the _main urls.py_ or shown file is included in some other _very main urls.py_? – Ivan Starostin May 20 '23 at 08:58
  • @IvanStarostin hii,i have added it in the code section ,and there is {% extends 'base.html' %} at the top of the template . – shafquet May 20 '23 at 09:23
  • Looks like Django handles the url but is unable to locate corresponding file in folder. There could be conflict between railway (i'm no expert in it) webserver config and django settings. Django is not supposed to handle media files on production. If you expect to see media files you uploaded locally then you have to deliver them manually to prod server in to MEDIA_ROOT folder. – Ivan Starostin May 20 '23 at 10:27

1 Answers1

0

I think you missed some code in urls.py. You have to include the necessary configurations for serving media files during development:

from django.conf.urls.static import static

if settings.DEBUG:
   urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Timus
  • 10,974
  • 5
  • 14
  • 28
RF-Mark
  • 1
  • 1