0

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

Emm
  • 2,367
  • 3
  • 24
  • 50

1 Answers1

0

Well, as you are using Apache to serve django, then why not use it serve static files as well. As per documetation, first you need to use collectstatic to store static files to a specific folder. For example in your settings if you have STATIC_ROOT specified as:

STATIC_ROOT = '/path/to/mysite.com/static/'

Then when you run collect static, the static files will be stored inside /path/to/mysite.com/static/ directory.

Then add that path as alias to apache:

Alias /static/ /path/to/mysite.com/static/
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • could be mistaken but this is what tried under attempt 2 – Emm Jan 23 '20 at 12:23
  • Did you ran collectstatic and also can you please verify that you have static files in your `/var/gradientboostmvp/static/` folder? – ruddra Jan 23 '20 at 12:47
  • yes I ran collectstatic and have my static files in that path – Emm Jan 23 '20 at 12:52
  • I am not good at this, but can you try giving permission to `static` route like this: ``` Require all granted ``` – ruddra Jan 23 '20 at 14:48