简体   繁体   English

OpenShift Django上的静态文件

[英]Static files on OpenShift Django

I'm having issues serving up static files - ie style sheets and images required from my html pages. 我在提供静态文件时遇到问题 - 即我的html页面需要样式表和图像。 I cannot seem to fathom why these static images are not being found. 我似乎无法理解为什么没有找到这些静态图像。 Here is what I have in my settings.py 这是我在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. 当我拖入日志时,我收到404错误。 But I cannot figure out where or debug or figure out where OpenShift is looking for these images. 但我无法弄清楚在哪里或调试或弄清楚OpenShift在哪里寻找这些图像。 I've tried using STATIC_URL = '/static/' but that doesn't work either. 我尝试过使用STATIC_URL = '/static/'但这也不起作用。

1) You've mixed up STATIC_URL and STATIC_ROOT variables. 1)你混淆了STATIC_URL和STATIC_ROOT变量。

STATIC_ROOT: Needs to point to the directory where you want your static files to be collected. STATIC_ROOT:需要指向您希望收集静态文件的目录。 In this case, it seems it is PROJECT_PATH + '/static/'. 在这种情况下,它似乎是PROJECT_PATH +'/ static /'。 Make sure this directory is correct. 确保此目录正确无误。

STATIC_URL: The url your static files are accessed. STATIC_URL:访问静态文件的URL。 /static/ is correct. / static /是正确的。

Remove the STATICFILES_DIRS variable. 删除STATICFILES_DIRS变量。 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. 它不能与STATIC_ROOT相同,因为它首先告诉Django在哪里寻找静态文件,然后告诉Django在收集静态文件时将它们存储在何处。

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. 2)如果你正在使用Openshift,正如@ timo.rieber指出的那样,你需要一个action_hook for deploy来告诉它收集你的静态文件。

3) Your static_root folder can't be anywhere in your project because Openshift webserver won't find it. 3)您的static_root文件夹不能位于项目的任何位置,因为Openshift webserver无法找到它。 It should be under yourproject/wsgi/static <--- (Thus, this will be the STATIC_ROOT variable value). 它应该在yourproject / wsgi / static <---下(因此,这将是STATIC_ROOT变量值)。

As I already answered you in another question, starting a new project from scratch for Openshift is painful. 正如我已在另一个问题中回答你的问题,从头开始为Openshift开始一个新项目是痛苦的。 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. 我写了一篇关于创建一个新项目以使其与Openshift一起工作的帖子 ,你也可以访问那里引用的提交,它会将你重定向到一个有效的基本Openshift项目。 You can also download basic project from Openshift repo but it follows an old (but still working) project structure. 您也可以从Openshift repo下载基本项目,但它遵循旧的(但仍在工作)项目结构。 You can now use a simpler project structure for python apps 您现在可以为python应用程序使用更简单的项目结构

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 只需转到settings.py并进行一些更改即可


replace 更换

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

WITH WITH

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

down at the end of settings.py file add this 在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'),
)

and in your html file just don't forget to load staticfiles like that 在你的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>

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

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 . 每次部署到生产中( git push到您的Openshift存储库)后,您需要将所有静态文件收集到一个地方,即STATIC_ROOT This is a job for the django management command collectstatic , see Django docs here . 这是django管理命令collectstatic ,请参阅Django docs

For Openshift environments a good practice to automate this after pushing upstream is the deploy action hook documented at the Openshift docs . 对于Openshift环境,在推送上游后自动执行此操作的一个好方法是在Openshift文档中记录的deploy操作挂钩。

When you Push... 当你推...

Building without Jenkins uses your application space as part of the build and test process. 没有Jenkins的构建使用您的应用程序空间作为构建和测试过程的一部分。 Because of this, the application is shut down so its memory can be used for building. 因此,应用程序将关闭,因此其内存可用于构建。 The following steps take place: 执行以下步骤:

  1. You run a 'git push' on your computer - your changes are sent to your OpenShift application 您在计算机上运行“git push” - 您的更改将发送到您的OpenShift应用程序
  2. The application is shut down 该应用程序已关闭
  3. Your changes are copied on the server into the correct location 您的更改将在服务器上复制到正确的位置
  4. OpenShift invokes your build hooks - script files you've placed in your Git repository OpenShift调用您的构建钩子 - 您放置在Git存储库中的脚本文件
  5. Your application is started 您的应用程序已启动

The deploy script may look like this: deploy脚本可能如下所示:

#!/bin/bash

source $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. 有关详细信息,您可能还会阅读Django无法找到的静态文件。 Need a second pair of eyes, I'm going crazy . 需要第二双眼睛,我会发疯的

I spent a long time trying to get static files working on Openshift. 我花了很长时间试图让静态文件在Openshift上运行。 Following this blog post I set up my Django project using the standard project layout but static files would not work. 在这篇博客文章之后,我使用标准项目布局设置了我的Django项目,但静态文件不起作用。 I think the vhost config that they use is not setup to look for files in /static/. 我认为他们使用的vhost配置并未设置为在/ 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. 这个问题在OpenShift中提供Django静态文件似乎暗示如果你使用旧的布局并使用wsgi / static它将起作用。 In the end I installed django-storages and served my static files from s3. 最后我安装了django-storages并从s3提供了我的静态文件。

Me also had the same problem with Heroku. 我也和Heroku有同样的问题。 Static files are not served correctly. 静态文件未正确提供。

Then i figured out every time when i deploy the application to heroku background its performing collectstatic. 然后,每当我将应用程序部署到heroku后台时,我都会想出它执行的collectstatic。 But i didnt mention STATIC_ROOT in settings.py. 但我没有在settings.py中提到STATIC_ROOT。

So i put static root like this 所以我把这样的静态root

STATIC_ROOT = 'staticfiles'

Then i deployed that time its working. 然后我部署了它的工作时间。 Try that out in openshift and let me know 在openshift中尝试一下,让我知道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM