DEBUG=False is not showing static and media files in Django project on Cpanel

119 Views Asked by At

I've looked into many articles and videos on this topic, but either I didn't understand or the solutions were insufficient. I would be very happy if you could help. I developed a Django project and deployed it on cPanel. My static and media files don't work when DEBUG = False. What should I do to make it work properly on cPanel? I'm not sure if Whitenoise is being used during the deployment phase. How can I run my Django project on cPanel in a suitable and fully functional manner? Here are my current codes:

settings.py:

DEBUG = True

ALLOWED_HOSTS = ['*']

STATIC_URL = '/static/'
STATIC_ROOT = '/home/directory/static/'
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/directory/media/'

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pages.urls')),
    path('', include('portfolio.urls')),
    path('', include('services.urls')),
    path('', include('blog.urls')),
    



] + static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

If you need any other info i can provide it. Thank you so much

3

There are 3 best solutions below

0
Colwin On

The django static files view is only meant to be used for development. Its not meant to be used for production deployments (typically where you set debug=False)

You should serve your static files with a webserver like apache

Take a look at the documentation here -> https://docs.djangoproject.com/en/5.0/howto/static-files/deployment/

You could also use white noise to serve the files like this

Install whitenoise

pip install WhiteNoise==2.0.6

Now change your wsgi.py file like this

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Please note this is not recommended and you should be serving the static files via apache/gunicorn.

0
MrMrProgrammer On

When the Django project is set to DEBUG = False, the media and static files will not be loaded. For this, you need to use servers like nginix and apache.

I hope the following page can help you. Github

0
Bedirhandd On

I dont know is it safe but i added those codes to my rootapp > urls.py and it just worked fine. I also used whitenoise too. Maybe it can be helpful for anyone too.

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

urlpatterns = [
    #Other url patterns
    re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
    re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}), ]