Static Files On Openshift Django
Solution 1:
1) You've mixed up STATIC_URL and STATIC_ROOT variables.
STATIC_ROOT: Needs to point to the directory where you want your static files to be collected. In this case, it seems it is PROJECT_PATH + '/static/'. Make sure this directory is correct.
STATIC_URL: The url your static files are accessed. /static/ is correct.
Remove the STATICFILES_DIRS variable. It can't be the same as STATIC_ROOT because first tells Django where to FIND static files and second tells Django where to STORE static files when they are collected.
2) If you are working with Openshift, as @timo.rieber points out, you will need an action_hook for deploy to tell it to collect your static files.
3) Your static_root folder can't be anywhere in your project because Openshift webserver won't find it. It should be under yourproject/wsgi/static <--- (Thus, this will be the STATIC_ROOT variable value).
As I already answered you in another question, starting a new project from scratch for Openshift is painful. I've wrote a post about creating a new project to make it work with Openshift and you can also visit the commit referenced there which will redirect you to a basic Openshift project which works. You can also download basic project from Openshift repo but it follows an old (but still working) project structure. You can now use a simpler project structure for python apps
Solution 2:
Hey I just had the same problem and I found a way to make it work.
Just go to your settings.py and make some changes
replace
DJ_PROJECT_DIR = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(DJ_PROJECT_DIR)
WITH
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
down at the end of settings.py file add this
STATIC_URL = '/static/'
TEMPLATE_DIRS = [
os.path.join(BASE_DIR, 'templates'),
]
STATIC_ROOT = os.path.join(BASE_DIR, '../static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static', 'dirs'),
)
and in your html file just don't forget to load staticfiles like that
<html><head> {% load staticfiles %}
<linkrel="stylesheet"type="text/css"href="{% static 'css/style.css' %}"></head><body><scriptsrc="{% static 'js/script.js' %}"></script>
It works! </body></html>
now your directories should look like that
--wsgi--myproject--templates-- drop html files here ... --static--dirs--img-- drop images here ..--js-- drop js here ..--css-- drop css files here ..
Solution 3:
After every deployment into production (git push
to your Openshift repository) you need to collect all static files to a single place, your STATIC_ROOT
. This is a job for the django management command collectstatic
, see Django docs here.
For Openshift environments a good practice to automate this after pushing upstream is the deploy
action hook documented at the Openshift docs.
When you Push...
Building without Jenkins uses your application space as part of the build and test process. Because of this, the application is shut down so its memory can be used for building. The following steps take place:
- You run a 'git push' on your computer - your changes are sent to your OpenShift application
- The application is shut down
- Your changes are copied on the server into the correct location
- OpenShift invokes your build hooks - script files you've placed in your Git repository
- Your application is started
The deploy
script may look like this:
#!/bin/bashsource$VIRTUAL_ENV/bin/activate
# Executing collectstatic (organize static files)
python "$OPENSHIFT_REPO_DIR"wsgi/yourproject/manage.py collectstatic --noinput
For further details you may also read Django cannot find static files. Need a second pair of eyes, I'm going crazy.
Solution 4:
I spent a long time trying to get static files working on Openshift. Following this blog post I set up my Django project using the standard project layout but static files would not work. I think the vhost config that they use is not setup to look for files in /static/. This question Serving Django static files in OpenShift seems to suggest if you use the old layout and use wsgi/static it will work. In the end I installed django-storages and served my static files from s3.
Solution 5:
Me also had the same problem with Heroku. Static files are not served correctly.
Then i figured out every time when i deploy the application to heroku background its performing collectstatic. But i didnt mention STATIC_ROOT in settings.py.
So i put static root like this
STATIC_ROOT = 'staticfiles'
Then i deployed that time its working. Try that out in openshift and let me know
Post a Comment for "Static Files On Openshift Django"