DEV Community

Cover image for Django Staticfiles and Media not showing when DEBUG = False [Fixed]
Odipo Otieno
Odipo Otieno

Posted on

Django Staticfiles and Media not showing when DEBUG = False [Fixed]

In urls.py in project *not * app urls.py:
imports:

from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns # new



from django.conf import settings

from django.views.static import serve

Enter fullscreen mode Exit fullscreen mode

urlspatterns :

urlpatterns = [
re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}),
]
Enter fullscreen mode Exit fullscreen mode

Settings.py


STATIC_URL = '/static/'

MEDIA_URL = '/media/'

if DEBUG:

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

else:

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

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

Enter fullscreen mode Exit fullscreen mode

It Works, Subscribe to my YouTube Channel KwargDevs

Top comments (2)

Collapse
 
fatihkurtl profile image
Fatih Kurt

Thanks man, it helped me a lot

Collapse
 
beta02 profile image
Sam

it help me a lot