Django 缓存导致 Azure 上缺少静态文件

Django Cache Causes Missing Staticfiles on Azure

提问人:Zhou Hang 提问时间:7/23/2023 最后编辑:Zhou Hang 更新时间:7/23/2023 访问量:29

问:

每当我添加以下代码时,Azure 上的 WhiteNoise Middlewire 都不会自动生成静态文件。

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': os.environ.get("REDIS_URL"),
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

当我pingdjango项目的/admin页面时,我遇到以下问题:

023-07-22T17:58:56.586800146Z 2023-07-22 17:58:56,536 ERROR    Internal Server Error: /admin/login/
2023-07-22T17:58:56.586846146Z Traceback (most recent call last):
2023-07-22T17:58:56.586855946Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/asgiref/sync.py", line 534, in thread_handler
2023-07-22T17:58:56.586862146Z     raise exc_info[1]
2023-07-22T17:58:56.586866746Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 42, in inner
2023-07-22T17:58:56.586871346Z     response = await get_response(request)
2023-07-22T17:58:56.586875846Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586880346Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/asgiref/sync.py", line 534, in thread_handler
2023-07-22T17:58:56.586884946Z     raise exc_info[1]
2023-07-22T17:58:56.586889046Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/core/handlers/base.py", line 284, in _get_response_async
2023-07-22T17:58:56.586910146Z     response = await sync_to_async(
2023-07-22T17:58:56.586915746Z                ^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586920146Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/asgiref/sync.py", line 479, in __call__
2023-07-22T17:58:56.586924446Z     ret: _R = await loop.run_in_executor(
2023-07-22T17:58:56.586928746Z               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586933046Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/asgiref/current_thread_executor.py", line 40, in run
2023-07-22T17:58:56.586937746Z     result = self.fn(*self.args, **self.kwargs)
2023-07-22T17:58:56.586942346Z              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586946546Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/asgiref/sync.py", line 538, in thread_handler
2023-07-22T17:58:56.586950946Z     return func(*args, **kwargs)
2023-07-22T17:58:56.586955146Z            ^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586959246Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/response.py", line 114, in render
2023-07-22T17:58:56.586963646Z     self.content = self.rendered_content
2023-07-22T17:58:56.586967746Z                    ^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586971846Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/response.py", line 92, in rendered_content
2023-07-22T17:58:56.586976146Z     return template.render(context, self._request)
2023-07-22T17:58:56.586980246Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586984346Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/backends/django.py", line 61, in render
2023-07-22T17:58:56.586988646Z     return self.template.render(context)
2023-07-22T17:58:56.586992846Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.586998546Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 175, in render
2023-07-22T17:58:56.587003046Z     return self._render(context)
2023-07-22T17:58:56.587007246Z            ^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587011346Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 167, in _render
2023-07-22T17:58:56.587015646Z     return self.nodelist.render(context)
2023-07-22T17:58:56.587020046Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587024246Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in render
2023-07-22T17:58:56.587028646Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587033046Z                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587042446Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in <listcomp>
2023-07-22T17:58:56.587048147Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587052547Z                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587056847Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 966, in render_annotated
2023-07-22T17:58:56.587061247Z     return self.render(context)
2023-07-22T17:58:56.587065547Z            ^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587069647Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/loader_tags.py", line 157, in render
2023-07-22T17:58:56.587078247Z     return compiled_parent._render(context)
2023-07-22T17:58:56.587082547Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587086747Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 167, in _render
2023-07-22T17:58:56.587091347Z     return self.nodelist.render(context)
2023-07-22T17:58:56.587095547Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587099747Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in render
2023-07-22T17:58:56.587104147Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587108447Z                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587112747Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in <listcomp>
2023-07-22T17:58:56.587117247Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587121747Z                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587126347Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 966, in render_annotated
2023-07-22T17:58:56.587131047Z     return self.render(context)
2023-07-22T17:58:56.587135247Z            ^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587139547Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/loader_tags.py", line 157, in render
2023-07-22T17:58:56.587143847Z     return compiled_parent._render(context)
2023-07-22T17:58:56.587148347Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587152547Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 167, in _render
2023-07-22T17:58:56.587156947Z     return self.nodelist.render(context)
2023-07-22T17:58:56.587161047Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587165447Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in render
2023-07-22T17:58:56.587169847Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587178947Z                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587183847Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in <listcomp>
2023-07-22T17:58:56.587188247Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587192747Z                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587331847Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 966, in render_annotated
2023-07-22T17:58:56.587358447Z     return self.render(context)
2023-07-22T17:58:56.587364547Z            ^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587368847Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/loader_tags.py", line 63, in render
2023-07-22T17:58:56.587373447Z     result = block.nodelist.render(context)
2023-07-22T17:58:56.587377847Z              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587382247Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in render
2023-07-22T17:58:56.587386847Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587391247Z                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587395847Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 1005, in <listcomp>
2023-07-22T17:58:56.587400847Z     return SafeString("".join([node.render_annotated(context) for node in self]))
2023-07-22T17:58:56.587405547Z                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587411647Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/template/base.py", line 966, in render_annotated
2023-07-22T17:58:56.587416547Z     return self.render(context)
2023-07-22T17:58:56.587421047Z            ^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587425747Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/templatetags/static.py", line 116, in render
2023-07-22T17:58:56.587430547Z     url = self.url(context)
2023-07-22T17:58:56.587434847Z           ^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587439247Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/templatetags/static.py", line 113, in url
2023-07-22T17:58:56.587444247Z     return self.handle_simple(path)
2023-07-22T17:58:56.587448647Z            ^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587452847Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/templatetags/static.py", line 129, in handle_simple
2023-07-22T17:58:56.587457647Z     return staticfiles_storage.url(path)
2023-07-22T17:58:56.587461847Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587475447Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/contrib/staticfiles/storage.py", line 203, in url
2023-07-22T17:58:56.587480847Z     return self._url(self.stored_name, name, force)
2023-07-22T17:58:56.587485447Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587489947Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/contrib/staticfiles/storage.py", line 182, in _url
2023-07-22T17:58:56.587494647Z     hashed_name = hashed_name_func(*args)
2023-07-22T17:58:56.587499147Z                   ^^^^^^^^^^^^^^^^^^^^^^^
2023-07-22T17:58:56.587503747Z   File "/tmp/8db8adb47bb0e7e/antenv/lib/python3.11/site-packages/django/contrib/staticfiles/storage.py", line 513, in stored_name
2023-07-22T17:58:56.587508447Z     raise ValueError(
2023-07-22T17:58:56.587512847Z ValueError: Missing staticfiles manifest entry for 'admin/css/base.css'

与生产文件上的静态文件相关的设置:

DEBUG = False
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    # Add whitenoise middleware after the security middleware
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    .....,
]

STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)
STATIC_URL = 'static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# WHITENOISE_USE_FINDERS = True
# WHITENOISE_MANIFEST_STRICT = False
# WHITENOISE_ALLOW_ALL_ORIGINS = True
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

我将代码重置为未使用缓存的时间,它可以工作,但是当我重新添加 cachce 设置并运行时

python manage.py collectstatic

,我收到相同的错误消息。在我添加缓存设置并删除缓存设置后,Azure 神奇地返回相同的错误消息(即使缓存设置不存在...... 我切换到本地内存缓存,但仍然无法正常工作.....

我无法弄清楚。任何帮助将不胜感激!

azure django-staticfiles redis-cache whitenoise django-redis

评论


答: 暂无答案