Host Whimsy Blog

Explore expert insights and tips on enhancing your digital footprint, from SEO strategies to securing the ideal domain for your brand.

Getting a Django Application to work - fixing the .htaccess configuration

Getting a Django Application to work - fixing the .htaccess configuration

Setting up Passenger_WSGI.py and .htaccess file in order to get the django website running. 


This guide will help you make sure all static files are loaded correctly on your django application.:

- First, create a python application from cPanel or DirectAdmin.
- After that, go to the terminal and make sure to "migrate" and "collectstatic"
- Ensure "Debug" has  been set to "False" in settings.py
- Restart the Django Application.
- In the file manager, go to the passenger_wsgi.py file and update it to the following:
 

import os
import sys

path = '/home/usr/domain.com/ProjectFolderName'  # use your actual path
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'ProjectName.settings'  # use your actual settings

from ProjectName.wsgi import application

Ensure to replace Path with actual path, and ProjectFolder and ProjectName with the actual Project Folder name and Project Name. 
 

After that, fix the .htaccess file in order to get your static files loading and application working smoothly. Place the .htaccess file in the root folder of the website if it is not already there.

.htaccess file:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"


# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/usr/domain.com"
PassengerBaseURI "/"
PassengerPython "/home/usr/virtualenv/domain.com/3.9/bin/python"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END

# Newly added text below here, in order to troubleshoot CSS not showing up
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^static/(.*)$ /home/usr/domain.com/staticfiles/$1 [QSA,L,NC]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^media/(.*)$ /home/usr/domain.com/mediaroot/$1 [QSA,L,NC]
</IfModule>


<FilesMatch "\.(?i:ico|flv|jpg|jpeg|png|gif|js|css)$">
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 month"
    </IfModule>
    FileETag MTime Size
</FilesMatch>

 

Replace "home/usr/domain.com paths with your own correct paths. 
Replace "staticfiles" and "mediaroot" with the correct directory names and path. 
 

 

Hopefuly this will get your site loading correctly if you were previously having issues with loading the static content.

 

Share on Facebook