python manage.py 收集静态不起作用引发“utf-8”编解码器无法解码字节 0xff 问题

问题描述

大家好,我一直在尝试将我的 django 应用程序部署到 heroku,但我遇到了一些问题。

下面是我在 settings.py 文件中的一些静态设置

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesstorage'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'  # this takes us to 'src/media/'    not 'static/media'

下面的 2 行产生相同的错误

python manage.py collectstatic
heroku run python manage.py collectstatic

python manage.py collectstatic错误

(venv) nmj@pc-nm:~/PROJECTS/abc/b99/mysite/src$ python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:

    /home/nmj/PROJECTS/abc/blueMust/mysite/src/staticfiles

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue,or 'no' to cancel: yes
Traceback (most recent call last):
  File "/home/nmj/PROJECTS/abc/blueMust/mysite/src/manage.py",line 22,in <module>
    main()
  File "/home/nmj/PROJECTS/abc/blueMust/mysite/src/manage.py",line 18,in main
    execute_from_command_line(sys.argv)
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/core/management/__init__.py",line 419,in execute_from_command_line
    utility.execute()
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/core/management/__init__.py",line 413,in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/core/management/base.py",line 354,in run_from_argv
    self.execute(*args,**cmd_options)
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/core/management/base.py",line 398,in execute
    output = self.handle(*args,**options)
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",line 187,in handle
    collected = self.collect()
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",line 128,in collect
    for original_path,processed_path,processed in processor:
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/whitenoise/storage.py",line 148,in post_process_with_compression
    for name,hashed_name,processed in files:
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/whitenoise/storage.py",line 88,in post_process
    for name,processed in files:
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py",line 406,in post_process
    yield from super().post_process(*args,**kwargs)
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py",line 231,processed,_ in self._post_process(paths,adjustable_paths,hashed_files):
  File "/home/nmj/PROJECTS/abc/SubmissionMgtSyst/mysite/venv/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py",line 288,in _post_process
    content = original_file.read().decode("utf-8")   # original line
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

下面是第 2 行 heroku run python manage.py collectstatic

产生的错误
(venv) nmj@pc-nm:~/PROJECTS/abc/b99/mysite/src$ heroku run python manage.py collectstatic
Running python manage.py collectstatic on ⬢ myapp... up,run.6053 (Free)
Traceback (most recent call last):
  File "/app/manage.py",in <module>
    main()
  File "/app/manage.py",in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py",in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py",in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py",**cmd_options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py",**options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",in handle
    collected = self.collect()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py",processed in processor:
  File "/app/.heroku/python/lib/python3.9/site-packages/whitenoise/storage.py",processed in files:
  File "/app/.heroku/python/lib/python3.9/site-packages/whitenoise/storage.py",processed in files:
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py",line 399,**kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py",hashed_files):
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py",line 282,in _post_process
    content = original_file.read().decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

我已经做了什么。 我跑了heroku config:set disABLE_COLLECTSTATIC=1 然后我打开了错误日志中的 storage.py 文件并尝试将 line 288 in error log 1 or line 282 in error log 2 both as shown above content = original_file.read().decode("utf-8") 更改为 content = original_file.read().decode("utf-16") 但得到了同样的错误。 然后我做了 content = original_file.read().decode()content = original_file.read() 并得到了一系列错误。 我到处搜索,但可用的解决方案适用于当您尝试从 .csv 文件中读取时,例如在 Pandas 中说 我还没有找到解决我的具体问题的解决方案。

有人请帮助我,因为这个 heroku 东西从教程中看起来很简单,但由于我什至不知道的原因,它变得非常耗时。

任何帮助将不胜感激。

解决方法

在 settings.py 中添加静态文件目录路径。然后运行 ​​collectstatic 命令。

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,"static"),]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...