在生产中配置Django中的静态文件

时间:2018-04-09 12:35:48

标签: django

我在Django 2.0.3中配置静态文件有困难当DEBUG = True时,部署在Heroku上的生产服务器正常工作,但是当我将其切换到DEBUG = False时,我收到应用程序错误,产生9相同的错误(外部IP):内部服务器错误电子邮件。

进一步让我感到困惑的是,本地开发服务器设置为DEBUG = True,但使用href =" {%static" images / apple-icon-57x57.png& #34; %}"显示Amazon S3的完整路径href =" https://r2cp-assets.s3.amazonaws.com/static/images/apple-icon-57x57.png"。我以为DEBUG = True会从本地服务器提供静态文件?在本地开发服务器上设置DEBUG = False不会导致任何问题。

我使用storages.backends.s3boto3进行静态和媒体存储。

运行Collectstatic不会返回任何错误。有没有什么东西会导致创建清单条目的问题?

我尝试过设置和不设置STATIC_ROOT。

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

这是DEBUG = False时来自prod服务器的错误消息的文本。

Internal Server Error: /

ValueError at /
Missing staticfiles manifest entry for 'images/apple-icon-57x57.png'

Request Method: GET
Request URL: https://obscure-lake-48617.herokuapp.com/
Django Version: 2.0.3
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.6.4
Python Path: ['/app', '/app/.heroku/python/bin', '/app', '/app/.heroku/python/lib/python36.zip', '/app/.heroku/python/lib/python3.6', '/app/.heroku/python/lib/python3.6/lib-dynload', '/app/.heroku/python/lib/python3.6/site-packages', '/app/r2cp', '/app/r2cp/apps']
Server time: Sun, 8 Apr 2018 17:22:58 -0400
Installed Applications:
('admin_tools',
 'admin_tools.theming',
 'admin_tools.menu',
 'admin_tools.dashboard',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.humanize',
 'whitenoise.runserver_nostatic',
 'django.contrib.staticfiles',
 'debug_toolbar',
 'imagekit',
 'storages',
 'main')
Installed Middleware:
('whitenoise.middleware.WhiteNoiseMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/r2cp/apps/main/views.py" in home
  51.         'overview': overview

File "/app/.heroku/python/lib/python3.6/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in render
  106.         url = self.url(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in url
  103.         return self.handle_simple(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in handle_simple
  118.             return staticfiles_storage.url(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in url
  155.         return self._url(self.stored_name, name, force)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in _url
  134.                 hashed_name = hashed_name_func(*args)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in stored_name
  422.                 raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

During handling of the above exception (Missing staticfiles manifest entry for 'images/apple-icon-57x57.png'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
  95.             response = self.get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  37.             response = response_for_exception(request, exc)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in response_for_exception
  87.         response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in handle_uncaught_exception
  126.     return callback(request, **param_dict)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/defaults.py" in server_error
  75.     return HttpResponseServerError(template.render())

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in render
  106.         url = self.url(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in url
  103.         return self.handle_simple(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in handle_simple
  118.             return staticfiles_storage.url(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in url
  155.         return self._url(self.stored_name, name, force)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in _url
  134.                 hashed_name = hashed_name_func(*args)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in stored_name
  422.                 raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

During handling of the above exception (Missing staticfiles manifest entry for 'images/apple-icon-57x57.png'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
  95.             response = self.get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  37.             response = response_for_exception(request, exc)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in response_for_exception
  87.         response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in handle_uncaught_exception
  126.     return callback(request, **param_dict)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/defaults.py" in server_error
  75.     return HttpResponseServerError(template.render())

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in render
  106.         url = self.url(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in url
  103.         return self.handle_simple(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in handle_simple
  118.             return staticfiles_storage.url(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in url
  155.         return self._url(self.stored_name, name, force)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in _url
  134.                 hashed_name = hashed_name_func(*args)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in stored_name
  422.                 raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

During handling of the above exception (Missing staticfiles manifest entry for 'images/apple-icon-57x57.png'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
  95.             response = self.get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  37.             response = response_for_exception(request, exc)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in response_for_exception
  87.         response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in handle_uncaught_exception
  126.     return callback(request, **param_dict)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/defaults.py" in server_error
  75.     return HttpResponseServerError(template.render())

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in render
  106.         url = self.url(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in url
  103.         return self.handle_simple(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in handle_simple
  118.             return staticfiles_storage.url(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in url
  155.         return self._url(self.stored_name, name, force)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in _url
  134.                 hashed_name = hashed_name_func(*args)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in stored_name
  422.                 raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

During handling of the above exception (Missing staticfiles manifest entry for 'images/apple-icon-57x57.png'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
  95.             response = self.get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  37.             response = response_for_exception(request, exc)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in response_for_exception
  87.         response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in handle_uncaught_exception
  126.     return callback(request, **param_dict)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/defaults.py" in server_error
  75.     return HttpResponseServerError(template.render())

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in render
  106.         url = self.url(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in url
  103.         return self.handle_simple(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in handle_simple
  118.             return staticfiles_storage.url(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in url
  155.         return self._url(self.stored_name, name, force)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in _url
  134.                 hashed_name = hashed_name_func(*args)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in stored_name
  422.                 raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

During handling of the above exception (Missing staticfiles manifest entry for 'images/apple-icon-57x57.png'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
  95.             response = self.get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  37.             response = response_for_exception(request, exc)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in response_for_exception
  87.         response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in handle_uncaught_exception
  126.     return callback(request, **param_dict)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/defaults.py" in server_error
  75.     return HttpResponseServerError(template.render())

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  175.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/test/utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  943.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  910.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in render
  106.         url = self.url(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in url
  103.         return self.handle_simple(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/templatetags/static.py" in handle_simple
  118.             return staticfiles_storage.url(path)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in url
  155.         return self._url(self.stored_name, name, force)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in _url
  134.                 hashed_name = hashed_name_func(*args)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py" in stored_name
  422.                 raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

Exception Type: ValueError at /
Exception Value: Missing staticfiles manifest entry for 'images/apple-icon-57x57.png'

1 个答案:

答案 0 :(得分:0)

我最终解决了我的问题。我怀疑问题是从命令行运行collectstatic会在Heroku中激活一个单独的dyno,一旦dyno结束,文件就会消失。我在我的应用程序的Heroku设置中禁用了collectstatic,似乎删除此配置并让构建过程运行collectstatic修复了所有内容。