我有一个Django应用程序可以在本地运行静态文件。当我推送到Heroku时,它会在不同的位置(home/www_dev/www_dev/settings/static)寻找静态文件。
文件结构:
home
> www_dev
>>> organizations (my app)
>>> static
>>> www_dev
>>>>> settings
base.py settings:(使用Two Scoops of Django模板)
STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
normpath(join(SITE_ROOT, 'static')),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
production.py settings(使用Heroku文档)
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
wsgi.py
import os
from os.path import abspath, dirname
from sys import path
SITE_ROOT = dirname(dirname(abspath(__file__)))
path.append(SITE_ROOT)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www_dev.settings.production")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
Procfile
web: gunicorn --pythonpath www_dev www_dev.wsgi -b 0.0.0.0:$PORT
尝试添加url .py每个其他堆栈溢出帖子,不工作:
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.contrib.staticfiles.views.serve', {'document_root': settings.STATIC_ROOT}),
)
Heroku错误:
-----> Preparing static assets
Collectstatic configuration error. To debug, run:
$ heroku run python ./www_dev/manage.py collectstatic --noinput
运行collectstatic:
结果OSError: [Errno 2] No such file or directory: '/app/www_dev/www_dev/settings/static'
我愿意去S3如果我不能解决这个问题,但有困难找到一个好的工作流程,推动静态(CSS/JS)本地S3与django-storage和boto。将发现所有媒体在S3上。任何帮助都非常感激!
在settings/production中更改STATIC_ROOT
。py
如果您不需要使用不同的静态根目录,您可以简单地删除变量,因为它已经在设置/base.py
中定义了。heroku文档假设您使用单个settings.py文件,该文件位于project_dir/settings.py中。