DEV Community

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

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 (5)

Collapse
 
fatihkurtl profile image
Fatih Kurt

Thanks man, it helped me a lot

Collapse
 
beta02 profile image
Sam

it help me a lot

Collapse
 
ashimghoshfm profile image
Ashim Ghosh

Thanks Boss

Collapse
 
ryeoun profile image
Rafhael Lowix Homwad

is there an update about this since python has new version?

Collapse
 
vincod profile image
Odipo Otieno (KwargDevs) • Edited

I'll try it on the new version then update you