【问题标题】:ValueError: Missing staticfiles manifest entry for 'favicon.png' when DEBUG = FalseValueError:当 DEBUG = False 时缺少“favicon.png”的静态文件清单条目
【发布时间】:2020-02-01 11:47:29
【问题描述】:

raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name) ValueError:当 DEBUG = False 时缺少“favicon.png”的静态文件清单条目

我只在 DEBUG = False 时得到这个错误,当 DEBUG = True 时我没有得到任何错误

要在保持 DEBUG = False 的同时解决此问题,我必须将 favicon.png(我之前删除过)添加回 static_root 文件夹,然后运行 ​​python manage.py collectstatic

我检查了所有文件,所有 html 文档都将链接 favicon.png 行注释掉了,所以这不是问题。

settings.py 有以下内容:

STATIC_URL = '/static/'

STATICFILES_DIRS =[
    os.path.join(BASE_DIR, 'static_root'),
]

VENV_PATH = os.path.dirname(BASE_DIR)

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

urls.py 有以下内容:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

【问题讨论】:

    标签: django django-staticfiles django-static


    【解决方案1】:

    我按照以下配置设置来解决问题。

    DEBUG = False
    
    ALLOWED_HOSTS = ['testnewapp.herokuapp.com']
    
    INSTALLED_APPS = [
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'whitenoise.runserver_nostatic',
        'django.contrib.staticfiles',
        'widget_tweaks',
        'phonenumber_field',
        'django_extensions',
    ]
    
    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware',
        ...
    ]
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/2.2/howto/static-files/
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
         os.path.join(BASE_DIR, "static"),
    ]
    
    # Whitenoise Storage Class  - Apply compression but don’t want the caching behaviour
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
    
    # Comment the below line
    # django_heroku.settings(locals())
    
    
    

    要记住的事情

    1. 确保您使用静态模板标签来引用您的静态文件,而不是直接编写 URL。例如:
    {% load static %}
    <img src="{% static "images/error.jpg" %}" alt="OOps!" />
    
    <!-- DON'T WRITE THIS -->
    <img src="/static/images/error.jpg" alt="OOps!" />
    
    1. 如果您收到有关 collectstatic 的错误消息,只需指示 Heroku 在部署过程中忽略运行 manage.py collectstatic 命令即可禁用它。

    【讨论】:

      【解决方案2】:

      是的,我也遇到了同样的问题。尝试运行manage.py collectstatic。 顺便说一句,即使 DEBUG 设置为 False,您也应该在控制台中打开日志记录。 Django documentation 有一篇文档文章。 如果您没有在生产环境中使用不同的静态文件存储,而是在本地运行静态文件,您可能应该从 urls.py 中删除 if settings.DEBUG:。 希望这有帮助!

      更新: 另外我已在项目的生产版本中从 settings.py 中删除了 STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

      【讨论】:

        猜你喜欢
        • 2020-11-19
        • 2017-10-24
        • 2018-10-05
        • 2020-01-15
        • 2023-03-06
        • 2020-11-18
        • 1970-01-01
        • 2021-06-06
        • 2018-08-28
        相关资源
        最近更新 更多