Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

python - Static files on OpenShift Django

I'm having issues serving up static files - i.e. style sheets and images required from my html pages. I cannot seem to fathom why these static images are not being found. Here is what I have in my settings.py

    STATIC_URL = PROJECT_PATH + '/static/'

# Additional locations of static files
STATICFILES_DIRS = (PROJECT_PATH + "/static/",)

STATIC_ROOT = "/static/"


# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

I'm getting a 404 error, when I tail into the log. But I cannot figure out where or debug or figure out where OpenShift is looking for these images. I've tried using STATIC_URL = '/static/' but that doesn't work either.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...