我的应用程序在我的本地正常运行,我可以运行
python manage.py collectstatic --noinput
没有错误。但是在构建过程结束时推送到Heroku时,我收到以下错误:
$ python manage.py collectstatic --noinput
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 193, in handle
collected = self.collect()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 115, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
for path in utils.get_files(storage, ignore_patterns):
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/files/storage.py", line 399, in listdir
for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/tmp/build_732715d9b29ba88f9eb56ca3d7e722de/MY_REAL_APP_NAME/static'
所以我的构建被拒绝了。我查看了OSError: [Errno 2] No such file or directory: '/tmp/MakeCalls/Static'但是当我删除STATICFILES_DIRS
时,应用程序将部署,但我的所有静态资产都会出现404错误。我按照Heroku exactly上的说明操作,但它似乎无法正常工作。我settings.py
文件的相关部分如下:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
ALLOWED_HOSTS = ['*']
和我的wsgi.py
文件:
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fortuno.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
这一切似乎都适用于我的本地,当我运行heroku local web
时。知道什么可能导致收集静态错误吗?
答案 0 :(得分:1)
DJANGO的静态文件处理
在settings.py 中
import os
def root(folder):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',folder)
在项目根目录
中创建static / media文件夹MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
root('static'),
)
项目根urls.py(project / project / urls.py)[django版本1.10如果你没有使用这个版本请不要再使用]
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
在本地计算机上运行python manage.py collectstatic
,如果它在项目中创建了staticstorage目录,那就完成了....继续部署........