【问题标题】:Error import whitenoise错误导入白噪声
【发布时间】:2018-11-06 05:58:42
【问题描述】:

我正在尝试按照 djangoGirls 教程在 pythonanywhere.com 中部署我的网站。

当我运行我的网站时,它给了我这个错误:

运行 WSGI 应用程序出错

ImportError: 没有名为“whitenoise”的模块

文件“/var/www/cryptoassistant_pythonanywhere_com_wsgi.py”,第 7 行,在模块中>

从 whitenoise.django 导入 DjangoWhiteNoise

我的 WGSI 文件:

import os
import sys
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

from whitenoise.django import DjangoWhiteNoise

application = DjangoWhiteNoise(get_wsgi_application())

path = '/home/cryptoassistant/tfg/'
if path not in sys.path:
    sys.path.append(path)

还有我的 setting.py 文件:

MIDDLEWARE_CLASSES = (
    'django.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

我安装了白噪声

$ pip install whitenoise
> Requeriment already satisfied: whitenoise in path (3.3.1)

我应该改变什么?

编辑

我安装了 freeze 和我的要求:

-f /usr/share/pip-wheels
Django==1.8
freeze==1.0.10
six==1.11.0
whitenoise==3.3.1

【问题讨论】:

  • 检查是否已经安装:$ pip freeze
  • @FadyAlfred 该命令返回:-f /usr/share/pip-wheels Django==1.8 whitenoise==3.3.1 所以,我使用:pip install freeze 并返回:-f /usr/ share/pip-wheels Django==1.8 freeze==1.0.10 六==1.11.0 whitenoise==3.3.1 以后我检查我的页面是否有效,不,这仍然相同
  • 你在requirements.txt文件中添加了whitenoise packega吗?
  • @SHIVAMJINDAL 我用我的需求文件编辑了我的问题
  • 您是否将 whitenoise 安装到 virtualenv 中?如果是这样,您是否在 PythonAnywhere 内的“Web”页面上指定了 virtualenv 路径?

标签: python django python-import whitenoise


【解决方案1】:

如果你使用虚拟环境,检查(command : pip freeze) 是否安装了whitenoise。如果是,您可以将以下详细信息添加到您的 settings.py 中。

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

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATIC_URL = '/static/'

STATIC_ROOT =  os.path.join(BASE_DIR,'staticfiles')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
#    '/var/www/static/',
]

如上所述验证/更新您的静态设置。

接下来,确保 DEBUG=True 并运行 collectstatic 命令。

要查看是否有任何错误,请运行 makemigrations 和 migrate 命令。你应该很好

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 2013-03-31
    • 2016-02-07
    • 2017-05-06
    • 2021-01-16
    • 2018-10-23
    • 2018-02-14
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多