Solving Heroku Collectstatic error while deploying Django app to Heroku

Heroku Collectstatic error while deploying Django app to Heroku is a very popular error which occurs when you try to deploy Django app to Heroku.

I will explain why this error is taking place and how to fix it, while also trying to add other solutions that could help us solve the problem.

Exploring the Heroku Collectstatic error while deploying Django app to Heroku

This is an error which occurs when you try to deploy Django app to Heroku.

Do not mix between errors. Make sure the error message looks like the error message bellow after double checking.

                                                                       #
Error while running '$ python manage.py collectstatic --noinput'.
...
Push rejected, failed to compile Python app
                                                                       #

In the sections bellow we will explain the source of the problem and propose many possible fixes.

Solution 1 : Correctly deploy Django app to Heroku

This is a very popular error which happens sometimes when you deploy a Django app to Heroku.

To run collectstatic you should be using the command bellow

                                                                       #
heroku run 'bower install --config.interactive=false;grunt prep;python manage.py collectstatic --noinput'
                                                                       #

but before we do that we should correctly deploy collectstatic.

The first thing you should know is that collectstatic should be disactivated during the Django deploy

                                                                       #
heroku config:set DISABLE_COLLECTSTATIC=1
                                                                       #

Then make sure to do the same for future deploys

                                                                       #
heroku config:unset DISABLE_COLLECTSTATIC
                                                                       #

Now, use the command bellow to deploy

                                                                       #
git push heroku master
                                                                       #

Use the command bellow in order to run migrations

                                                                       #
heroku run python manage.py migrate
                                                                       #

After that you should try running collecstatic. I hope the method above has fixed your problem, try the one bellow if that is not the case.

Solution 2 : Fix all collectstatic related issues before Heroku deployment

The next method is a step by step fix. The goal of this method is to fix all collectstatic related issues with a few commands.

First, we should start with this command

                                                                       #
python manage.py collectstatic
                                                                       #

Then run the test command bellow

                                                                       #
python manage.py test
                                                                       #

Finally, in order to deploy use this command

                                                                       #
git push heroku master
                                                                       #

I hope the fix above fixed your problem. Thank you for reaching the end of this blog post

Summing-up : 

This is the end of our article, I hope the solutions I presented worked for you, Learning Python is a fun journey, do not let the errors discourage you. Keep coding and cheers.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/