Search code examples
djangodjango-urlsdjango-staticfilesdjango-media

Media Files and Static files not Displaying and working in Django


when deployed server then static files not working and media files not diplaying. 404 error

here is the urls.py

from django.views.static import serve
import django
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path ('' , include('home.urls'))
]  +  static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and settings.py

STATIC_URL = 'static/'
STATIC_ROOT = '/usr/local/lsws/Example/html/demo/static'

"""STATICFILES_DIRS=(
    BASE_DIR / "static",
   
)"""

MEDIA_URL = 'media/'
MEDIA_ROOT = '/usr/local/lsws/Example/html/demo/static/media'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CKEDITOR_UPLOAD_PATH = 'uploads'

Solution

  • Verify that on prod it's the same folder and the user running webserver has access to this folder. Consider using BASE_DIR instead of absolute paths. Also you might prefer to store projects files not inside user private home folder and store them somewhere /var/opt or whatever. If you had media files locally before deployment then they must be copied to prod manually. – Thank you @Ivan-Starostin