python manage.py collect static not working. Raises a 'utf-8' codec can't decode byte 0xff issue

1k Views Asked by At

Hi guys so i have been trying to deploy my django application to heroku and i ran through a couple of issues.

Below is some of the static setting that i have in my settings.py file

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'

The 2 lines below generate the same error.

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

The error for python manage.py collectstatic is

(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, hashed_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, in post_process
    for name, hashed_name, 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

Below is the error generated by the 2nd line 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", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, 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", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
    collected = self.collect()
  File "/app/.heroku/python/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 "/app/.heroku/python/lib/python3.9/site-packages/whitenoise/storage.py", line 148, in post_process_with_compression
    for name, hashed_name, processed in files:
  File "/app/.heroku/python/lib/python3.9/site-packages/whitenoise/storage.py", line 88, in post_process
    for name, hashed_name, processed in files:
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py", line 399, in post_process
    yield from super().post_process(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py", line 231, in post_process
    for name, hashed_name, processed, _ in self._post_process(paths, adjustable_paths, 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

What i have already done. I ran heroku config:set DISABLE_COLLECTSTATIC=1 Then i did open the storage.py file in the error log and tried changing the line 288 in error log 1 or line 282 in error log 2 both as shown above content = original_file.read().decode("utf-8") to content = original_file.read().decode("utf-16") but got same error. Then i did content = original_file.read().decode() and content = original_file.read() and got a chain of errors. I have searched everywhere but the solutions available are for when you're trying to read from a .csv file like say in pandas I have not found a solution talking about my specific kind of problem.

Someone please help me because this heroku thing looks straightforward from the tutorials but is turning out to be something really time consuming for a reason i dont even know.

Any help will be greatly appreciated.

1

There are 1 best solutions below

2
On

Add Static files directory path in the settings.py. And then run the collectstatic command.

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