【问题标题】:Heroku with whitenoise not serving django staticfiles带有白噪声的 Heroku 不提供 django 静态文件
【发布时间】:2021-11-27 00:06:58
【问题描述】:

如何让 heroku 在我的 django 项目中提供静态文件?

好的,在推送到 heroku 时,我收到与静态文件相关的消息:

$ python manage.py collectstatic --noinput
remote:        130 static files copied to '/tmp/build_48c64d55/staticfiles', 410 post-processed.

但是当我访问我的站点时,清除静态文件无法识别并且网络选项卡显示:

GET https://pure-everglades-09529.herokuapp.com/static/style.css   404 Not Found

这是我的 html 的内容,引用了我找不到的 css:

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
    <title>{% block title %}My amazing site{% endblock %}</title>
</head>

这是我的 settings.py 的底部,它的配置就像到处说的那样:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

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

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# Activate Django-Heroku.
django_heroku.settings(locals())

在我的设置中,我已经按照建议添加了白噪声依赖项:

INSTALLED_APPS = [
    'myapp.apps.MyappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    # Disable Django's own staticfiles handling in favour of WhiteNoise, for
    # greater consistency between gunicorn and `./manage.py runserver`. See:
    # http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
]

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

最后,这是我的网址,我在其中添加了静态网址,但仅适用于我的本地环境:

urlpatterns = [
    path('', include('myapp.urls')),
    path('admin/', admin.site.urls),
]

if 'Owner' in settings.BASE_DIR:
    urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=setting

我做错了什么?是因为我在设置中有 DEBUG=True 吗?我将其设置为 False 作为检查,但出现 500 错误

【问题讨论】:

    标签: django heroku django-staticfiles whitenoise


    【解决方案1】:

    显然

    # Extra places for collectstatic to find static files.
    STATICFILES_DIRS = [
        os.path.join(PROJECT_ROOT, 'static'),
    ]
    

    需要

    # Extra places for collectstatic to find static files.
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
    ]
    

    静态文件仍然在正确的位置创建,可能是因为 VARIABLES 被

    覆盖
    django_heroku.settings(locals())
    

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 1970-01-01
      • 2017-09-09
      • 2023-03-07
      • 2021-12-30
      • 1970-01-01
      • 2012-12-03
      • 2017-09-02
      • 2012-08-13
      相关资源
      最近更新 更多