相关疑难解决方法(0)

在Heroku上找不到Django静态文件(带有whitenoise)

好像有几次问这个问题,但我无法解决.

我在生产中部署了一个django应用程序DEBUG = False.我设定了我的allowed_host.我{% load static from staticfiles %}以前加载静态文件.我正好写了Heroku doc提取的设置:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

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

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Run Code Online (Sandbox Code Playgroud)

但是我得到了一个错误500.并得到了这个追溯(通过邮件)

...
`cache_name = self.clean_name(self.hashed_name(name))
 File "/app/.heroku/python/lib/python3.5/site-    packages/django/contrib/staticfiles/storage.py", line 94, in hashed_name (clean_name, self))
...
ValueError: The file ‘app/css/font.css’ could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7febf600a7f0>.`
Run Code Online (Sandbox Code Playgroud)

当我运行heroku run python manage.py collectstatic --noinput All似乎没问题:

276 static files copied to '/app/annuaire/staticfiles', …

python django heroku django-staticfiles

12
推荐指数
4
解决办法
8111
查看次数

当 Debug=False 时,Django Heroku 不提供静态文件

我在Heroku上托管我的Django应用程序并使用白噪声来处理静态文件。

以下是settings.py的内容

DEBUG = False

ALLOWED_HOSTS += [
    'example.com',
]

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

STATIC_URL = '/static/'

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

STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'static_root')
Run Code Online (Sandbox Code Playgroud)

但是静态文件不起作用。

设置Debug=True为静态文件提供服务,但在Debug=False.

django heroku whitenoise

3
推荐指数
1
解决办法
2152
查看次数

标签 统计

django ×2

heroku ×2

django-staticfiles ×1

python ×1

whitenoise ×1