【问题标题】:Configuring static files in Django in production在生产环境中在 Django 中配置静态文件
【发布时间】:2018-09-18 20:42:36
【问题描述】:

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

让我更困惑的是,本地开发服务器设置为 DEBUG = True,但使用 href="{% static "images/apple-icon-57x57.png" %}" 的静态文件的路径显示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'

【问题讨论】:

    标签: django


    【解决方案1】:

    我终于解决了我的问题。我怀疑问题是从命令行运行 collectstatic 会在 Heroku 中启动一个单独的测功机,一旦测功机结束,文件就会消失。我在我的应用程序的 Heroku 设置中禁用了 collectstatic,看来删除此配置并让构建过程运行 collectstatic 修复了所有问题。

    【讨论】:

      猜你喜欢
      • 2014-10-13
      • 2015-11-26
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 2013-01-22
      • 2017-12-30
      • 2018-07-26
      相关资源
      最近更新 更多