Accessing Static Files in Django (WSGI) Hosted on NameCheap

Resolving an issue that static files can’t be reached (i.e., files not found, 404) when using Django running on a NameCheap shared machine.

I bumped into an issue when trying to set up a Django + React environment by following this tutorial on my NameCheap space.
No matter how I tried configuring Django’s settings.py, the static files in my Django app just can’t be reached.
Nothing was mentioned in the tutorial for any potential issues like this, until I found an answer in this post.

It turns out to be a WSGI problem and it has nothing to do with Django’s or React’s configurations.
By making changes in my wsgi.py like the following, I can successfully access the static files:

wsgi.py
import os
from django.conf import settings
from django.contrib.staticfiles.handlers import StaticFilesHandler
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
if settings.DEBUG:
    application = StaticFilesHandler(get_wsgi_application())
else:
    application = get_wsgi_application()

where you need to replace the 'project.settings' string with your project name’s.

References

Was this post helpful?

Leave a Reply

Your email address will not be published.