Collectstatic error while deploying Django app to Heroku

前端 未结 10 1827
长情又很酷
长情又很酷 2020-12-04 06:34

I\'m trying to deploy a Django app to Heroku, it starts to build, download and installs everything, but that\'s what I get when it comes to collecting static files



        
相关标签:
10条回答
  • 2020-12-04 07:12

    DO NOT disable collectstatic on heroku with heroku config:set DISABLE_COLLECTSTATIC=1. This will just hide the error and not make your app healthy.

    Instead, it's better to understand why the collectstatic command fails because it means something is not right with your settings.

    Step 1

    Run locally both commands:

    python manage.py collectstatic
    python manage.py test
    

    You should see one or more error messages. Most of the time, it's a missing variable (for ex: STATIC_ROOT) you must add to your project settings.py file.

    It's necessary to add the test command because some collectstatic related issues will only surface with test, such as this one

    Step 2

    Once you've fixed all the error messages locally, push again to heroku.

    Troubleshooting

    Remember you can also run commands directly in your heroku VM. If you cannot reproduce locally, run the collecstatic command in heroku and check what's going on directly in your production environment:

    python manage.py collectstatic --dry-run --noinput
    

    (Same goes for heroku console obviously)

    0 讨论(0)
  • 2020-12-04 07:12

    Heroku had made a document with suggestions on how to handle this https://devcenter.heroku.com/articles/django-assets

    add to settings.py

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    

    make a directory in the root of your project called staticfiles, put a favicon or something in there, just make sure git tracks it. Then the collectstatic command should finish on heroku.

    0 讨论(0)
  • 2020-12-04 07:26

    You have STATICFILES_DIRS configured to expect a static directory in the same directory as your settings.py file, so make sure it's there not somewhere else.

    Also, do you have any files in that static directory? If you don't then git won't track it and so although it exists locally it won't exist in git. The usual solution to this is to create an empty file called .keep in the directory which will ensure that git tracks it. But once you have some static files in this directory then it won't be a problem anymore.

    0 讨论(0)
  • 2020-12-04 07:27

    Today, not all of the requirements came in properly with $ pipenv install django from the heroku-django-template and $ pip install -r requirements.txt.

    The latest version of the template includes a /static folder with a humans.txt, so the previous solution is likely not the proplem

    Try running $ pipenv install whitenoise and then $ pip freeze > requirements.txt.

    If that works, I would recommend $ pip install psycopg2 --ignore-installed and $ pip freeze > requirements.txt as well, otherwise you will similarly have problems migrating.

    0 讨论(0)
  • 2020-12-04 07:27

    I faced the same issue while deploying my app. I realized I had updated my pip version, installed few plugins but forgot to create a fresh requirements.txt file.

    Run pip freeze > requirements.txt in your terminal
    Run python manage.py collectstatic
    Now push the code to github and deploy to heroku server

    Hope this helps if that is the case

    0 讨论(0)
  • 2020-12-04 07:29

    Run python manage.py collectstatic locally and fix any errors. In my case there were reference errors that prevented that command from running successfully.

    0 讨论(0)
提交回复
热议问题