I have moved my django project from development to my production server. After deployment I cannot view static files on my pages.
I know that django does not serve static files once debug is turned off and have tried using whitenoise to serve my static files.
Attempt 1- WhiteNoise
These are the changes that I made to settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
and wsgi.py:
from whitenoise.django import DjangoWhiteNoise
...
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
and then running collectstatic. However my webpages still were not loading any of the static files.
Attempt 2- Apache- mod_wsgi
With the second attempt, I tried using apache+ mod_wsgi To be specific I connected to my VPS using terminal in cpanel installed apache2 and followed a tutorial.
sudo apt-get install apache2
created my conf file
sudo nano new_config.conf
added this to it
<VirtualHost *:80>
ServerName 127.0.0.1
ServerAlias localhost
Alias /static /var/gradientboostmvp/static/
WSGIScriptAlias / /var/gradientboostmvp/django_school/wsgi.py
<Directory /var/gradientboostmvp/>
Order deny,allow
Allow from all
</Directory>
DocumentRoot /var/gradientboostmvp
</VirtualHost>
enabled the newly created virtual host conf file
sudo a2ensite new_config.conf
sudo /etc/init.d/apache2 restart
added my WSGIPythonPath in apache2.config
WSGIPythonPath /var/gradientboostmvp
saved the changed, but still couldn't load my static files
Attempt 3- Import serve
I also had a similar question that was closed as a duplicate. I tried the solutions offered in the answers
from django.views.static import serve
...
urlpatterns = [
path('', classroom.home, name='home'),
path('about', classroom.about, name='about'),
path('courses', classroom.courses, name='courses'),
path('course_details', classroom.course_details, name='course_details'),
path('static',serve {'document_root':settings.STATIC_ROOT}),
Which then resulted in me getting an error message when I tried visiting my home page
Incomplete response received from application