【问题标题】:When deploy on Heroku, report TypeError: expected str, bytes or os.PathLike object, not tuple在 Heroku 上部署时,报告 TypeError: expected str, bytes or os.PathLike object, not tuple
【发布时间】:2018-05-16 23:28:25
【问题描述】:

我用命令禁用collectstatic

heroku config:set DISABLE_COLLECTSTATIC=1

成功将我的项目推送到 Heroku 后,手动 collectstatic 如下:

$ heroku run python manage.py collectstatic

不幸的是,Heroku 报告错误涉及manage.py

Running python manage.py collectstatic on ⬢ fierce-cove-94300... up, run.6296 (Free)
/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 173, in handle
    if self.is_local_storage() and self.storage.location:
  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py", line 239, in inner
    return func(self._wrapped, *args)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/files/storage.py", line 283, in location
    return abspathu(self.base_location)
  File "/app/.heroku/python/lib/python3.6/posixpath.py", line 371, in abspath
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not tuple

settings.py

#Heroku Setting
cwd = os.getcwd()
if cwd == "/app" or cwd[:4] == "/tmp":
    import dj_database_url
    DATABASES = { #DATABASES plurual
        "default": dj_database_url.config(default="postgres://localhost"),
    }

    #Honor the "X-Forwarded-Proto" header for request.is_secure().
    SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", 'https')

    # Allow all host headers
    ALLOWED_HOSTS = ['*']

    #Static asset configuration
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    STATIC_ROOT = (BASE_DIR, "staticfiles")
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )

如何解决这样的问题?

【问题讨论】:

    标签: python django heroku


    【解决方案1】:

    错误在您的STATIC_ROOT 设置中。正如错误消息所述,您传递的是元组而不是路径:

    STATIC_ROOT = (BASE_DIR, "staticfiles")
    

    改成:

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

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2021-09-12
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      相关资源
      最近更新 更多