Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
disruptive Avatar asked Jul 06 '14 11:07

disruptive


People also ask

Where are Django static files stored?

Files are searched by using the enabled finders . The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

Does Django serve static files?

django.contrib.staticfiles provides a convenience management command for gathering static files in a single directory so you can serve them easily. This will copy all files from your static folders into the STATIC_ROOT directory. Use a web server of your choice to serve the files.

How does Django load static files?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.

What is static and media files in Django?

As Uku Loskit said, static files are for things like your applications' css files, javascript files, images, etc. Media files are typically user or admin uploadable files. Normally you will want MEDIA_ROOT and STATIC_ROOT to be separate directories.


2 Answers

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

like image 140
argaen Avatar answered Sep 21 '22 12:09

argaen


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 %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
</head><body> 
         <script src="{% 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 ..
like image 39
OmarSSelim Avatar answered Sep 23 '22 12:09

OmarSSelim