Reputation: 45
I am trying to add a css file to my django application which I am deploying on openshift. Currently, I am able to have the css file work when running locally with debug and all that. However, when I deploy my application to openshift, the css file does not have any effect. Many of the other questions online seem to be referring to old versions of django and openshift (lots of references to a wsgi
folder that doesn't seem to be standard anymore and triggering collectstatic
manually)
My understanding with the current django/openshift relationship is that I do not need to do collecstatic manually as I am using the django-ex template from openshift, and during the build process the collectstatic command is run automatically, the ouput of which I can see in the logs. Notably, I can see that my style.css file for my application gets copied into STATIC_ROOT in that log.
My project structure from my project root is thus:
.
├── db.sqlite3
├── djangoWrapper
│ ├── __init__.py
│ ├── settings.py
│ ├── templates
│ │ └── djangoWrapper
│ │ └── index.html
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── gitlab-ci.yml
├── ldap
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── static
│ │ └── ldap
│ │ └── style.css
│ ├── templates
│ │ └── ldap
│ │ ├── apiOffline.html
│ │ ├── index.html
│ │ ├── results.html
│ │ └── search.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── manage.py
├── openshift
├── README.md
├── requirements.txt
Here's what I am doing in my settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "ldap", "static"),]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
In my html files, I am then trying to load the css file with
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'ldap/style.css' %}">
in the <head>
section.
I have attempted to do some debugging here because it would be assumed that the problem before I got to deployment was that my static files were not being put in the correct place/djgano couldn't find them. So I put some prints into my app and deployed it on openshift and got back the following info:
BASE_DIR: /opt/app-root/src
STATIC_ROOT: /opt/app-root/src/static
STATIC_URL: /static/
APP_VIEW_DIR: /opt/app-root/src/ldap/views.py
as well putting <p>"{%static 'ldap/style.css' %}"</p>
into an html file, which displays as /static/ldap/style.css
From these tidbits, I know that the root directory for openshift is /opt/app-root/src/
, that my static files are in /opt/app-root/src/static
, and that my html is looking for the css file in /static/ldap/style.css
. This seems like it should be working then as all of those directories line up. Is there something else about serving static files with django/openshift that I am missing here?
Upvotes: 0
Views: 312
Reputation: 45
Finally got it working! I did use whitenoise, following the steps here.
Upvotes: 1