- 这是django应用程序,我使用的是DigitalOcean应用程序平台用于部署我的应用程序。当我设置
DJANGO_DEBUG=True
(在DigitalOcean上的环境变量中)所有的静态文件都是由服务器找到的,我的应用程序工作正常。当我设置
DJANGO_DEBUG=False,
那么一切都错了:
- 我得到500个错误代码
- 当我签出"运行时日志"时有相同的错误信息:
**ValueError: Missing staticfiles manifest entry for 'images/favicons/favicon.ico'**
[gumroad-clone] [2023-04-04 09:20:11]
[gumroad-clone] [2023-04-04 09:20:11] During handling of the above exception, another exception occurred:
[gumroad-clone] [2023-04-04 09:20:11]
[gumroad-clone] [2023-04-04 09:20:11] Traceback (most recent call last):
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
[gumroad-clone] [2023-04-04 09:20:11] response = get_response(request)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/utils/deprecation.py", line 134, in __call__
[gumroad-clone] [2023-04-04 09:20:11] response = response or self.get_response(request)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 58, in inner
[gumroad-clone] [2023-04-04 09:20:11] response = response_for_exception(request, exc)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 140, in response_for_exception
[gumroad-clone] [2023-04-04 09:20:11] response = handle_uncaught_exception(
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 185, in handle_uncaught_exception
[gumroad-clone] [2023-04-04 09:20:11] return callback(request)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/utils/decorators.py", line 134, in _wrapped_view
[gumroad-clone] [2023-04-04 09:20:11] response = view_func(request, *args, **kwargs)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/views/defaults.py", line 102, in server_error
[gumroad-clone] [2023-04-04 09:20:11] return HttpResponseServerError(template.render())
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/backends/django.py", line 61, in render
[gumroad-clone] [2023-04-04 09:20:11] return self.template.render(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 175, in render
[gumroad-clone] [2023-04-04 09:20:11] return self._render(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 167, in _render
[gumroad-clone] [2023-04-04 09:20:11] return self.nodelist.render(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
[gumroad-clone] [2023-04-04 09:20:11] return SafeString("".join([node.render_annotated(context) for node in self]))
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
[gumroad-clone] [2023-04-04 09:20:11] return SafeString("".join([node.render_annotated(context) for node in self]))
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
[gumroad-clone] [2023-04-04 09:20:11] return self.render(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/loader_tags.py", line 157, in render
[gumroad-clone] [2023-04-04 09:20:11] return compiled_parent._render(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 167, in _render
[gumroad-clone] [2023-04-04 09:20:11] return self.nodelist.render(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 1000, in render
[gumroad-clone] [2023-04-04 09:20:11] return SafeString("".join([node.render_annotated(context) for node in self]))
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 1000, in <listcomp>
[gumroad-clone] [2023-04-04 09:20:11] return SafeString("".join([node.render_annotated(context) for node in self]))
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/template/base.py", line 958, in render_annotated
[gumroad-clone] [2023-04-04 09:20:11] return self.render(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/templatetags/static.py", line 116, in render
[gumroad-clone] [2023-04-04 09:20:11] url = self.url(context)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/templatetags/static.py", line 113, in url
[gumroad-clone] [2023-04-04 09:20:11] return self.handle_simple(path)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/templatetags/static.py", line 129, in handle_simple
[gumroad-clone] [2023-04-04 09:20:11] return staticfiles_storage.url(path)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/storage.py", line 166, in url
[gumroad-clone] [2023-04-04 09:20:11] return self._url(self.stored_name, name, force)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/storage.py", line 145, in _url
[gumroad-clone] [2023-04-04 09:20:11] hashed_name = hashed_name_func(*args)
[gumroad-clone] [2023-04-04 09:20:11] File "/workspace/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/storage.py", line 465, in stored_name
[gumroad-clone] [2023-04-04 09:20:11] raise ValueError(
[gumroad-clone] [2023-04-04 09:20:11] ValueError: Missing staticfiles manifest entry for 'images/favicons/favicon.ico'
我正在使用这些依赖项:
**whitenoise==6.4.0**
**django==4.0.10** # pyup: < 4.1
pytz==2022.7.1
python-slugify==8.0.1
Pillow==9.4.0
argon2-cffi==21.3.0
redis==4.5.2
hiredis==2.2.2
stripe==5.3.0
django-environ==0.10.0
django-model-utils==4.3.1
django-allauth==0.53.1
django-crispy-forms==2.0
crispy-bootstrap5==0.7
django-redis==5.2.0
djangorestframework==3.14.0
django-cors-headers==3.14.0
drf-spectacular==0.26.1
gunicorn==20.1.0
psycopg2==2.9.5
在settings.py:
DJANGO_APPS = [
**'whitenoise.runserver_nostatic',**
**"django.contrib.staticfiles",**
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.messages",
# "django.contrib.humanize", # Handy template tags
"django.contrib.admin",
"django.forms",
]
THIRD_PARTY_APPS = [
"crispy_forms",
"crispy_bootstrap5",
"allauth",
"allauth.account",
"allauth.socialaccount",
"rest_framework",
"rest_framework.authtoken",
"corsheaders",
"drf_spectacular",
]
LOCAL_APPS = [
"djgumroad.users",
"djgumroad.products.apps.ProductsConfig",
]
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"corsheaders.middleware.CorsMiddleware",
"**whitenoise.middleware.WhiteNoiseMiddleware**",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
STATIC_ROOT = str(BASE_DIR / "staticfiles")
STATIC_URL = "/static/"
STATICFILES_DIRS = [str(APPS_DIR / "static")]
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
- 首先我试着检查是否有一个名为"staticfiles"在我的项目根(我想DigitalOcean应该运行
python manage.py collectstatic
用于每个部署和所有静态文件应该复制并收集到一个文件夹staticfiles/
但是当我检查部署的服务器时,我找不到任何名为&;staticfiles&;的文件夹。在根目录下(也许这就是为什么我的服务器在DEBUG=False时找不到任何静态文件-根本没有静态文件/文件夹!!!!)
- 在控制台中运行
python manage.py collectstatic
我得到这个错误信息:
**Post-processing 'assets/js/plugins/gmaps.min.js' failed!**
Traceback (most recent call last):
File "/workspace/manage.py", line 31, in <module> execute_from_command_line(sys.argv)
File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_comm and_line
utility.execute()
File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv (self.argv)
File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "/workspace/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute output self.handle(*args, **options)
File "/workspace/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", 1 ine 209, in handle
collected = self.collect()
File "/workspace/.heroku/python/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", l ine 154, in collect
raise processed
**whitenoise.storage.MissingFileError: The file 'assets/js/plugins/gmaps.min.js.map' could not be found with <whitenoise.storage.Com pressedManifestStaticFiles Storage object at 0x7f6b915bc190>.
The JS file 'assets/js/plugins/gmaps.min.js' references a file which could not be found:
assets/js/plugins/gmaps.min.js.map
Please check the URL references in this JS file, particularly any
relative paths which might be pointing to the wrong location.**
总结一下:
- 当DEBUG=False时,我的应用程序找不到静态文件
- 应该有staticfields/文件夹在我的项目根,但没有。
- 我想我需要有人来解释一下白噪声哪里做错了配置
你知道吗?
更新:
我发现这个问题是因为我有同样的问题。我终于找到并解决了这个问题:
首先,你需要有一个静态站点,正如我在第一个回答中所描述的,#2。参考https://docs.digitalocean.com/tutorials/app-deploy-django-app/#step-5-deploying-your-static-files .
问题的真正症结在于,你的白噪声设置被设置为压缩和缓存静态文件。缓存过程包括对文件名进行散列,以便它们可以永远缓存:https://whitenoise.readthedocs.io/en/latest/django.html#add-compression-and-caching-support。这样做会给每个静态文件一个唯一的URL &这个URL似乎是数字海洋应用平台的问题。
两个选项解决:
- 从您的项目中删除白噪声&允许数字海洋静态站点处理静态资产。这只需从settings.py文件中删除所有对白噪声的引用即可完成。注意:这样做可能会破坏本地实例上的图像,例如,如果您像我一样通过PyCharm运行本地测试站点。
- 通过更改settings.py中的这一行来更改白噪声存储引擎:
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
…它将压缩静态文件,但不会缓存或散列它们的名称。
选项2将允许您的静态文件在本地站点和应用程序平台上都可以看到,但我不知道它是否"正确",";因为它似乎在做双重任务。
对于我的应用程序,我在settings.py中添加了逻辑,以便仅在DEBUG=True时使用白噪声(这是我的本地实例的情况,但不是在我的Digital Ocean应用程序平台上)
初始回答:
这是一个老问题,但我注意到两件事:
-
STATIC_ROOT = str(BASE_DIR / "staticfiles")
应该是STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
(STATICFILES_DIRS
也一样) - Digital ocean不希望你的app-platform实例在生产中提供静态文件,所以当
DEBUG
为False时(当app-platform假设你不再调试时),你不会得到任何静态文件。您必须部署一个静态站点(它是免费的),以便在生产环境中提供静态文件:https://docs.digitalocean.com/tutorials/app-deploy-django-app/#step-5-deploying-your-static-files