django 为什么在DEBUG=False时找不到静态文件(在生产环境中)?

nbnkbykc  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(222)
  • 这是django应用程序,我使用DigitalOcean应用程序平台来部署我的应用程序。当我设置
DJANGO_DEBUG=True

字符串
(in DigitalOcean上的环境变量)所有的静态文件都被服务器找到了,我的应用程序工作正常。但是当我

DJANGO_DEBUG=False,


然后一切都错了
1.我得到500错误代码
1.当我 checkout “运行时日志”时,出现了相同的错误消息:

**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"


1.首先,我试图检查在我的项目根目录中是否有一个名为“staticfiles”的文件夹(我假设DigitalOcean应该运行

python manage.py collectstatic


对于每个部署,所有静态文件都应复制并收集到一个文件夹staticfiles/
但是当我检查部署的服务器时,我在根目录中找不到任何名为“staticfiles”的文件夹(也许这就是为什么当DEBUG=False时我的服务器找不到任何静态文件的原因-根本没有staticfiles/文件夹!!!!)
1.在控制台中运行

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.**


所以总结一下:
1.当DEBUG=False时,我的应用无法找到静态文件
1.在我的项目的根目录下应该有staticfiels/文件夹,但是没有。
1.我想我需要有人来解释一下WhiteNoise配置的错误
你知道吗?

6mzjoqzu

6mzjoqzu1#

这是一个老问题,但我注意到两件事:

  1. STATIC_ROOT = str(BASE_DIR / "staticfiles")应为STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")STATICFILES_DIRS也是如此)
  2. Digital ocean不希望您的应用平台示例在生产环境中提供静态文件,因此当DEBUG为False时(当应用平台假设您不再调试时),您将不会获得任何静态文件。您必须部署一个静态站点(免费)来在生产环境中提供静态文件:https://docs.digitalocean.com/tutorials/app-deploy-django-app/#step-5-deploying-your-static-files

相关问题