Django and OpenShift static files can't be found

梦想与她 提交于 2019-12-13 04:59:37

问题


So I followed this tutorial: https://github.com/rancavil/django-openshift-quickstart/wiki/Tutorial-How-create-an-application-with-Django-1.6-on-Openshift

and I tried to add a static image to the default home.html page and it just won't find it.

HTML:

{% load static %}
<html>
    <head>

    </head>
    <body>
        <img src="{% static 'Logo_Trans.png' %}">
        <div class="left"></div>
        <div class="right">
            <p>is coming soon</p>
        </div>
    </body>
</html>

All other files are as listed in the repo given.


回答1:


I've found the problem. The static files were serving properly when deployed but not when in debug mode. To fix this just add the STATICFILES_DIR variable in settings.py

Find:

if ON_OPENSHIFT:
     DEBUG = False
else:
     DEBUG = True

Add:

if ON_OPENSHIFT:
     DEBUG = False
else:
     DEBUG = True
     STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),
                            '/var/www/static/',)

Openshift is a very good service but definitely needs to update their docs/examples for django. Their sample program still runs django 1.4. Hope this helps anyone else that runs into this problem.




回答2:


you can read the answer for this question Django cannot find static files. Need a second pair of eyes, I'm going crazy. Hope it will help you to understand static files. Static files in Django are always a bit painful.

Btw, as I explain in the last comment, I recommend you to create an app called common within a static folder inside it to place static content that is not application specific. Static application specific files should be placed inside the static folder within your app. This way, you can forget about defining the STATICFILES_DIRS variable and it will always work in DEBUG mode.

After that, ofc, define STATIC_ROOT and when you want to work in deploy mode, do the collectstatic command and it will also work.

After dealing hundreds of times with issues like this, I've found this is the best approach.



来源:https://stackoverflow.com/questions/24398718/django-and-openshift-static-files-cant-be-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!