1

我正在尝试在我的产品评论网站中提供静态文件,并且我正在使用 Whitenoise,但它不起作用(无法在 /static 中找到文件)(当我在本地使用 DEFAULT = False 进行测试时,它仍然作品)

我尝试配置 wsgi 文件而不是使用白噪声中间件

这是我的设置文件中的一些代码,用于提供静态服务。

DEBUG = False

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]

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

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'djangobower.finders.BowerFinder',
)

你能告诉我如何解决它吗?请原谅我的英语

我尝试再次配置设置:

DEBUG = False

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# I don't have STATICFILES_DIRS, is it wrong?
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'djangobower.finders.BowerFinder',
)

但它仍然无法提供静态文件

4

2 回答 2

0

我相信你缺少的是STATICFILES_STORAGE. 这是我的settings.py相关配置。

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
ALLOWED_HOSTS = ["*"]
于 2019-10-02T06:36:29.387 回答
0

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

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 命令来禁用它。

但是如果你需要在任何 WSGI 应用程序中使用 WhiteNoise

  • 您需要将现有的 WSGI 应用程序包装在 WhiteNoise 实例中,并告诉它在哪里可以找到您的静态文件。例如:
from my_project import MyWSGIApp

application = MyWSGIApp()
application = WhiteNoise(application, root='/path/to/static/files')
application.add_files('/path/to/more/static/files', prefix='more-files/')

笔记

这些说明适用于任何 WSGI 应用程序。但是,对于 Django 应用程序,您最好使用 WhiteNoiseMiddleware 类,它使集成更容易。

#

http://whitenoise.evans.io/en/stable/base.html

于 2020-02-01T04:01:29.170 回答