Search code examples
pythondjangoherokucollectstatic

Heroku upload static folder not found


I was trying to reproduce the tutorial Django app and uploading it to Heroku server, but I can't resolve problems with static files.

Here is a link to all files on github: https://github.com/Rufus90/poll.git

When I try to run heroku run python manage.py collectstatic --noinput

I get this error:

Running python manage.py collectstatic --noinput on ⬢ hidden-plains-30510... up, run.1265 (Free)
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
    collected = self.collect()
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 105, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/finders.py", line 131, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/utils.py", line 23, in get_files
    directories, files = storage.listdir(location)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/files/storage.py", line 315, in listdir
    for entry in os.scandir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/static'

After completing the tutorial I have copied the whole repo, renamed it and changed the paths where I think it was needed. I was playing with my old project from tutorial but it got completely messed up and stopped working at all so I stuck to this one. Right now I can't even replace old static files with new ones or change font or anything and I don't know where the problem lies.

Can someone please explain me what did I do wrong? Besides copying the folder and renaming it..


Solution

  • As per heroku documentation, you need to create that directory /app/static inside your code base, so that it is available when you run collectstatic. Here is what documentation has mentioned:

    Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to create this directory in your codebase, so it will be available when collectstatic is run. Git does not support empty file directories, so you will have to create a file inside that directory as well.

    Alternatively you can use whitenoise in heroku. As django does not serve static files in production mode(when you set DEBUG=False in settings), here whitenoise becomes handy. It can serve static files in production mode. Integrating it in your django project is easy as well.

    First, install it using pip install whitenoise.

    Then add WhiteNoiseMiddleware in settings.py:

    MIDDLEWARE_CLASSES = (
        # Simplified static file serving.
        # https://warehouse.python.org/project/whitenoise/
        'whitenoise.middleware.WhiteNoiseMiddleware',
        ...
    

    Finally if you want GZIP enabled storage for static files then put the following line in settings.py file:

    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    

    More info on whitenoise can be found here.