Heroku 上带有 Gunicorn 服务器的 Django 项目不提供静态文件

标签 django heroku gunicorn static-files

我的 Django 1.3 项目在开发服务器上提供静态文件,但在 Gunicorn 服务器上不提供静态文件。我按照this Heroku guide的步骤进行操作.

当我像指南中一样使用 procfile 的内容时( web: Gunicorn myproject_django.wsgi -b 0.0.0.0:$PORT) Heroku 无法识别我的项目。

然后我将该 Procfile 更改为:

web: python myproject_django/manage.py run_gunicorn -b 0.0.0.0:$PORT -w 3

现在我的应用程序运行,除了静态文件(css 不活动,图像也不活跃)。

我的项目树:

.
├── Procfile
├── myproject_django
│   ├── core
│   │   ├── admin.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── static
│   │   │   ├── css
│   │   │   │   ├── base.css
│   │   │   │   ├── layout.css
│   │   │   │   
│   │   │   └── media
│   │   │       ├── pek.ico
│   │   │       ├── pek.png
│   │   │       ├── pek_symbol.png
│   │   ├── tests.py
│   │   └── views.py
│   ├── __init__.py
│   ├── manage.py
│   ├── settings.py
│   ├── templates
│   │   └── core
│   │       ├── home.html
│   │       └── install.html
│   └── urls.py
└── requirements.txt

settings.py 的潜在相关部分

MEDIA_ROOT = ''

MEDIA_URL = '/static/media'

STATIC_ROOT = ''

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
    os.path.abspath(__file__)+'/..'+'/myproject_django/core/static', 
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
    'gunicorn',
    'django.contrib.admin',
)

编辑

Francis Yaconiello 加入后我调整了以下内容:

在settings.py

STATIC_ROOT = os.path.join(os.getcwd(),'core/static')
STATICFILES_DIRS = ( os.path.abspath(__file__)+'/..'+'/core/static', )

在 gitignore 中:

staticfiles/*

然后就 promise 了。 最后运行 heroku run python myproject_django/manage.pycollectstatic

但是当我检查网页时仍然没有提供静态文件。 鉴于我的目录树,为什么这些更改不起作用?

编辑2

我仍然看不到我的静态文件。当我在 DEBUG=True 时单击图像时,我得到:

Request URL: http://myproject.herokuapp.com/static/media/pek.png

(请注意,staticfiles 目录为空)

.
├── Procfile
├── myproject_django
│   ├── admin
│   ├── core
│   │   ├── admin.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── static
│   │   │   ├── css
│   │   │   │   ├── base.css
│   │   │   │   ├── layout.css
│   │   │   └── media
|   |   |       ├── pek.ico
|   │   │       ├── pek.png
|   │   │       ├── pek_symbol.png
│   │   ├── tests.py
│   │   ├── views.py
│   ├── __init__.py
│   ├── manage.py
│   ├── settings.py
│   ├── staticfiles
│   ├── templates
│   │   └── core
│   │       ├── 404.html
│   │       ├── 500.html
│   │       ├── home.html
│   │       └── install.html
│   ├── urls.py
└── requirements.txt

在settings.py

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'static/media')
STATIC_ROOT = os.path.join(PROJECT_PATH,'staticfiles')
STATICFILES_DIRS = (
    os.path.join(PROJECT_PATH, 'core/static'),
)

最佳答案

指定您的STATIC_ROOT

我通常将其设置为:

import os
STATIC_ROOT = os.path.join(os.getcwd(), "staticfiles")

在您的项目中创建该目录

mkdir staticfiles

确保静态文件的内容未被 git 跟踪

nano .gitignore

添加

staticfiles/*

然后提交并推送到heroku。

最后

heroku run python manage.py collectstatic

编辑

STATIC_ROOT = os.path.join(os.getcwd(),'core/static')
STATICFILES_DIRS = ( os.path.abspath(__file__)+'/..'+'/core/static', )

这两个设置不能是同一件事。

STATICFILES_DIR 没问题,如果多余的话,您的 STATICFILES_FINDERS 已经告诉它在每个应用程序的 static 目录中查找静态文件:'django.contrib.staticfiles.finders.AppDirectoriesFinder',

STATIC_ROOT 的目的是提供一个与您的项目完全独立的新目录,您可以使用 nginx 等第三方服务器来提供服务。这就是您创建该目录 staticfiles 的原因:

STATIC_ROOT = os.path.join(os.getcwd(),'staticfiles')

另一次编辑

临时文件系统的解决方法

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

这意味着只要您在网络启动期间收集每个 dyno 实例上的静态数据,就不会有任何问题。

web: python myproject_django/manage.py collectstatic --noinput; python myproject_django/manage.py run_gunicorn -b 0.0.0.0:$PORT -w 3

也就是说,我目前使用 S3 和 django 存储 http://django-storages.readthedocs.org/en/latest/index.html 。实现这一点相当容易(而且便宜)。

关于Heroku 上带有 Gunicorn 服务器的 Django 项目不提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11994004/

相关文章:

python - 创建一个只显示(不可编辑)的 Django 管理字段

django - 如何使用 Tivix django-cron 应用程序

django - 通过反向外键关系过滤使用存在的元素

heroku - 用于网络分析的Heroku插件

reactjs - 尝试将我的 REACT Webpack 应用程序部署到 heroku。一切都在本地工作,heroku 构建工作。但是我得到了一个 404 nginx

django - 主管 Django Gunicorn Gevent 内存使用

python - Django 安装为本地应用程序

ruby-on-rails - 基本表单操作不适用于 heroku(但在本地有效)

python - ModuleNotFoundError - 尝试启动服务时没有名为 'main' 的模块

python - Nginx Django 和 Gunicorn。 Gunicorn socks 文件丢失了吗?