2

I deployed a django project on shared host , and my media files did not loadedproduct image not load

what config do i do for fix this issue?

setting.py

STATIC_URL = '/site_statics/'
STATICFILES_DIRS = [
   os.path.join(BASE_DIR, "assets")
]

STATIC_ROOT = os.path.join(BASE_DIR, "static_cdn", "static_root")

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR,  "static_cdn","media_root")
# MEDIA_ROOT = '/static_cdn/media_root'

urls.py:

urlpatterns = [
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
path('admin/', admin.site.urls),
path('<lang>/', home_page, name='home'),
path('', home_redirect),
path('<lang>/products/', include('products.urls')),
path('<lang>/', include('agents.urls')),
path('<lang>/', include('information.urls')),
path('<lang>/', include('media_app.urls')),

]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Will it shows if you run Django app locally? If you browser developer tool, what HTTP status error code shows for those missing images? 404, 403? – Eric Mar 03 '21 at 06:38
  • @Eric No,I dont.it Deployed on shared host – Hamid Reza Mojtahed Mar 08 '21 at 06:48
  • Django is not meant to serve media files in production. https://stackoverflow.com/questions/39051206/how-to-serve-media-files-on-django-production-environment – Ron Oct 06 '21 at 17:10

1 Answers1

0

You must configure the media context for your virtual host in LSWS settings.

  1. In your LSWS management GUI, click Virtual Hosts
  2. Click the name of the virtual host you are using (the default is Example)
  3. Click the Context tab
  4. Click the + in the right hand side of the page to add a new context
  5. Select the Type Static
  6. Set the URI to /media/ (trailing slash is necessary as this is a directory)
  7. Set the Location to your filesystem media folder location
  8. Set Accessible to Yes

You can refer to the static context for an example. It will be set up the same way but with different URI and root.

Ron
  • 1,450
  • 15
  • 27