在Heroku上使用gunicorn服务器的Django项目不提供静态文件

Ben*_*ey4 10 django heroku static-files gunicorn

我的Django 1.3项目服务于开发服务器上的静态文件,但没有提供gunicorn服务器静态文件.我按照Heroku指南的步骤进行操作.

当我使用我的proc文件的内容时,如在指南( 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
Run Code Online (Sandbox Code Playgroud)

现在我的应用运行除了静态文件(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
Run Code Online (Sandbox Code Playgroud)

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',
)
Run Code Online (Sandbox Code Playgroud)

编辑

在Francis Yaconiello的参赛作品后,我调整了以下内容:

在settings.py中

STATIC_ROOT = os.path.join(os.getcwd(),'core/static')
STATICFILES_DIRS = ( os.path.abspath(__file__)+'/..'+'/core/static', )
Run Code Online (Sandbox Code Playgroud)

在gitignore:

staticfiles/*
Run Code Online (Sandbox Code Playgroud)

然后承诺.终于跑了heroku run python myproject_django/manage.py collectstatic.

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

EDIT2

我仍然没有看到我的静态文件.当我DEBUG=True得到这个时点击图像:

Request URL: http://myproject.herokuapp.com/static/media/pek.png
Run Code Online (Sandbox Code Playgroud)

(注意staticfilesdir是空的)

.
??? 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
Run Code Online (Sandbox Code Playgroud)

在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'),
)
Run Code Online (Sandbox Code Playgroud)

Fra*_*llo 5

指定你的 STATIC_ROOT

我通常把它设置为:

import os
STATIC_ROOT = os.path.join(os.getcwd(), "staticfiles")
Run Code Online (Sandbox Code Playgroud)

在项目中创建该目录

mkdir staticfiles
Run Code Online (Sandbox Code Playgroud)

确保静态文件的内容不是git跟踪的

nano .gitignore
Run Code Online (Sandbox Code Playgroud)

staticfiles/*
Run Code Online (Sandbox Code Playgroud)

然后提交它并推送到heroku.

最后,

heroku run python manage.py collectstatic
Run Code Online (Sandbox Code Playgroud)

编辑

STATIC_ROOT = os.path.join(os.getcwd(),'core/static')
STATICFILES_DIRS = ( os.path.abspath(__file__)+'/..'+'/core/static', )
Run Code Online (Sandbox Code Playgroud)

这两个设置不可能是一回事.

STATICFILES_DIR没关系,如果多余你STATICFILES_FINDERS已经告诉它在static每个应用程序的目录中找到staticfiles :'django.contrib.staticfiles.finders.AppDirectoriesFinder',

重点STATIC_ROOT是提供一个完全独立于您的项目的新目录,您可以使用第三方服务器(如nginx)提供该目录.这就是您创建该目录的原因staticfiles:

STATIC_ROOT = os.path.join(os.getcwd(),'staticfiles')
Run Code Online (Sandbox Code Playgroud)

另一个编辑

临时文件系统的解决方法

每个dyno都有自己的短暂文件系统,并带有最近部署的代码的全新副本.在dyno的生命周期中,其运行进程可以将文件系统用作临时暂存器,但是任何其他dyno中的进程都不会看到所写的文件,并且在dyno停止或重新启动时,所写的任何文件都将被丢弃.

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

web: python myproject_django/manage.py collectstatic --noinput; python myproject_django/manage.py run_gunicorn -b 0.0.0.0:$PORT -w 3
Run Code Online (Sandbox Code Playgroud)

也就是说,我目前使用S3和django存储http://django-storages.readthedocs.org/en/latest/index.html.这很容易(也很便宜).