Static files in OpenShift Django

I am having problems maintaining static files, i.e. the stylesheets and images required from my html pages. I cannot understand why these static images were not found. That's what I have in mysettings.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 get 404 error when I fit into a log. But I can't figure out where to either debug or figure out where OpenShift is looking for these images. I tried using STATIC_URL = '/static/', but that also does not work.

+4
source share
5 answers

1) You have mixed up the variables STATIC_URL and STATIC_ROOT.

STATIC_ROOT: , . , , PROJECT_PATH + '/static/'. , .

STATIC_URL: , ./static/.

STATICFILES_DIRS. , STATIC_ROOT, Django, , Django, STORE , .

2) Openshift, @timo.rieber, action_hook , , .

3) static_root , - Openshift . /wsgi/static < --- ( , STATIC_ROOT).

, Openshift - . , Openshift, , Openshift, . Openshift repo, ( ) . python

+7

, , .

settings.py


DJ_PROJECT_DIR = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(DJ_PROJECT_DIR)

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

settings.py

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'),
)

html

<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>

--wsgi
    --myproject
        --templates
            -- drop html files here ... 
        --static
            --dirs
                --img
                    -- drop images here ..
                --js
                    -- drop js here ..
                --css
                    -- drop css files here ..
+4

(git push Openshift) , STATIC_ROOT. django collectstatic, . Django docs here.

Openshift deploy, Openshift.

...

Jenkins . - , . :

  • "git push" - OpenShift
  • .
  • OpenShift - script , Git

deploy script :

#!/bin/bash

source $VIRTUAL_ENV/bin/activate

# Executing collectstatic (organize static files)
python "$OPENSHIFT_REPO_DIR"wsgi/yourproject/manage.py collectstatic --noinput

Django . , .

+1

, Openshift. Django , . , vhost, , /static/. Django OpenShift, , , wsgi/static, . django-storages s3.

+1

Heroku. .

Then I found out every time I deploy the application on a background hero, performing its collection. But I did not mention STATIC_ROOT in settings.py.

So, I put a static root like this

STATIC_ROOT = 'staticfiles'

Then I launched his work at this time. Try this in openhift and let me know

0
source

Source: https://habr.com/ru/post/1547305/


All Articles