Reputation: 149
Static files don't load to the django website deployed on fastcomet, with cpanel
The site is deployed and seemly works well in the other regards, loads from the database, redirects to pages but it doesn't not load any css, javascript or images, it's bare html, I have tried changing {% load static %}
to {% load staticfiles %}
in all pages, and vice-versa and setting direct directory to the staticfiles folder in the settings file, it works perfectly fine in my localhost, I don't know what could be the problem.
Static and Media files in settings.py
file:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
wsgi.py file
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'aida_ganaderia_luz_y_sombra.settings')
application = get_wsgi_application()
passenger_wsgi.py
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'aida_ganaderia_luz_y_sombra/wsgi.py')
application = wsgi.application
current state:
Upvotes: 1
Views: 1454
Reputation: 116
It is nice to meet a customer here in the Stack :)!
The issue you are experiencing is indeed caused by cPanel and the way how it handles python scripts. Probably every cPanel web hosting provider will also have if not the same then similar issues, so I believe this answer can be useful to all cPanel Python users.
I will try to keep the technical tone more softer since the matter involve quite a lot of technical subjects.
Let's start by explaining how Python and cPanel work together.
When you create a python application, cPanel creates two things:
Using this information we can outline two services that are taking part in the communication between your visitors and your python app - the web service and the python service.
The web service is simply mapping your domain to the python app start script using the .htaccess file stored in the document root of your domain and then the python service is processing your application's code logic. Once the result of the application's code is ready, python sends the result back to the web service which finally sends it to the visitor in the form of simple HTML file. (Not so different than how php actually works!)
What happens to the static file requests and why are those resulting in 404 status code?
When it comes to the static files the situation with those is different. Immediately after an HTML response is sent to your visitor, the HTML DOM tree is loaded in their browser.
Then the browser makes a request for every static resource used on the page - images, js files, css files and so on. However, the static resources are not handled by python so the web server recognize the request as a simple one that do not need routing to your python application.
In other words the web server expects that the static resources are located on the document root of your domain. However, they are not since the document root where all your python application files are deployed is different than the document root your domain is configured to work with. I know it sounds complicated, but it is really a matter of understanding the technology and configuring your application.
How to resolve the error and make your application great again?
You have two choices here.
For example:
The primary domain for your account is example.com. It is configured to work with the public_html directory (document root) of your account which translates to the following absolute path:
/home/user/public_html
Your python app is deployed in a folder outside that one - for example MyPythonApp with the following absolute path:
/home/user/MyPythonApp
The folder holding your static resources is initially within the folder of the python app:
/home/user/MyPythonApp/static/
And the request to the static resource that results in 404 error look like that:
GET http://example.com/static/styles.css
By simply moving the "static" folder from /home/user/MyPythonApp/static to /home/user/public_html/ you will allow for the request to correctly fetch the resource.
For this task however you will need our help and for that I would like to kindly ask you to submit a ticket to our Technical Support Team so they can help you with that. If you prefer this option, please send them reference to this Question.
Best Regards!
Upvotes: 4