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/ayvacioglu/ayvacioglu_v2/static/'
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/ayvacioglu/ayvacioglu_v2/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
2
Answers
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
Now change your wsgi.py file like this
Please note this is not recommended and you should be serving the static files via apache/gunicorn.
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