【问题标题】:Using Whitenoise but still can not serve static files使用 Whitenoise 但仍无法提供静态文件
【发布时间】:2019-10-02 04:11:13
【问题描述】:

我正在尝试在我的产品评论网站中提供静态文件,并且我正在使用 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',
)

但还是不能提供静态文件

【问题讨论】:

  • 如果在本地机器上测试结果不一样,能否分享一下nginx/Apache的配置,看看有没有什么问题会干扰?

标签: django product whitenoise


【解决方案1】:

我相信您缺少的是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 = ["*"]

【讨论】:

  • 你能告诉我你配置 ALLOW_HOST 的那一行吗,我正在尝试放置 staticfiles_storage 但它返回代码 500、400。当我运行 collectstatic 时(我删除了所有静态和静态文件文件)它返回错误找不到静态目录(代码和你一样)。谢谢
  • 我允许我的 ALLOWED_HOSTS 上的所有内容,因为它是一个测试项目。我已经编辑了我的答案。
  • 我试过了,但是当我在评论网站上启动它时,它返回错误 500,当我删除 staticfiles_storage 时,它​​可以再次打开网站(但无法加载静态文件)。你能告诉我如何解决这个问题吗?
  • 我使用STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"解决了错误500,它可以工作但仍然无法加载静态文件
  • 酷。这是我第一次使用,所以不知道这个包的确切输入/输出
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-13
    • 2022-11-10
    • 2021-11-25
    • 2021-06-03
    • 1970-01-01
    • 2020-11-11
    • 2011-07-22
    • 2019-01-19
    相关资源
    最近更新 更多