【问题标题】:Issue with static files when deploying Django app to Heroku将 Django 应用程序部署到 Heroku 时出现静态文件问题
【发布时间】:2021-07-23 15:12:15
【问题描述】:

我的项目文件夹如下所示:

├── Procfile
├── core
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   └── views.cpython-39.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── inspirationSources.txt
├── manage.py
├── package-lock.json
├── package.json
├── react-frontend
│   ├── README.md
│   ├── build
│   │   ├── asset-manifest.json
│   │   ├── favicon.ico
│   │   ├── index.html
│   │   ├── logo192.png
│   │   ├── logo512.png
│   │   ├── manifest.json
│   │   ├── robots.txt
│   │   └── static
│   ├── package-lock.json
│   ├── package.json
│   ├── public
│   │   ├── favicon.ico
│   │   ├── index.html
│   │   ├── logo192.png
│   │   ├── logo512.png
│   │   ├── manifest.json
│   │   └── robots.txt
│   └── src
│       ├── App.css
│       ├── App.js
│       ├── App.test.js
│       ├── assets
│       ├── components
│       ├── hooks
│       ├── index.css
│       └── index.js
├── requirements.txt
├── spotify
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── admin.cpython-39.pyc
│   │   ├── apps.cpython-39.pyc
│   │   ├── cluster.cpython-39.pyc
│   │   ├── credentials.cpython-39.pyc
│   │   ├── models.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   ├── util.cpython-39.pyc
│   │   └── views.cpython-39.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── cluster.py
│   ├── credentials.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   ├── models.py
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   ├── util.py
│   └── views.py
├── spotifycluster
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-39.pyc
│   │   ├── settings.cpython-39.pyc
│   │   ├── urls.cpython-39.pyc
│   │   └── wsgi.cpython-39.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── tutorialSources.txt

当我使用 git push heroku main 进行部署时,它似乎很好,但是当我使用补充 url 在浏览器中打开应用程序时,屏幕上出现以下错误(调试模式已打开):

TemplateDoesNotExist at /
build/index.html
Request Method: GET
Request URL:    https://nameless-taiga-02413.herokuapp.com/
Django Version: 3.1.7
Exception Type: TemplateDoesNotExist
Exception Value:    
build/index.html
Exception Location: /app/.heroku/python/lib/python3.9/site-packages/django/template/loader.py, line 19, in get_template
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.9.4
Python Path:    
['/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python39.zip',
 '/app/.heroku/python/lib/python3.9',
 '/app/.heroku/python/lib/python3.9/lib-dynload',
 '/app/.heroku/python/lib/python3.9/site-packages']
Server time:    Fri, 30 Apr 2021 10:08:26 +0000
Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.filesystem.Loader: /app/react-frontend/build/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.9/site-packages/django/contrib/admin/templates/build/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/templates/build/index.html (Source does not exist)

我的 settings.py 中有以下设置

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

我的中间件列表中还有白噪声

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...]

settings.py 中的模板部分如下所示

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'react-frontend')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

我希望 Heroku 能够根据 STATIC_DIRS 变量找到静态文件夹,但事实并非如此。知道这里发生了什么吗?

【问题讨论】:

    标签: django heroku


    【解决方案1】:

    所以实际上这是一个非常愚蠢的错误。 react-frontend 是一个损坏的子模块,我需要通过确保 .git/config 没有子模块并从缓存中删除 react-frontend (No submodule mapping found in .gitmodules for path and missing .gitmodules file) 来修复它。再次提交并推送,它起作用了......

    【讨论】:

      【解决方案2】:

      如果您的 DEBUG 设置为 False Django 则无法处理 STATIC FILESHeroku 提供一些配置来服务STATIC FILES

      第 1 步:安装白噪声

      $ pip install whitenoise
      

      STEP-2 : 默认情况下签入settings.py whitenoise 中间件,如果不是添加它,它在MIDDLEWARE_CLASSES 中可用。

      MIDDLEWARE = (
          'whitenoise.middleware.WhiteNoiseMiddleware',
          ...)
      

      第 3 步:将其添加到您的 settings.py

      STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
      

      【讨论】:

      猜你喜欢
      • 2012-07-08
      • 2016-12-23
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2020-12-17
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多