DEV Community

loading...

Django and Heroku

Afshar Mohebi
A work-life balance lover developer who is passionate about startups while trying to keep updated with coding trends.
・2 min read

I have used Heroku for hosting a Node.js/Express application once before. It was really easy. I created a project in Heroku, chained it to my Github project and indicated it as an automatic build. All I did was this. I just saw Heroku identifying my language and platform correctly, building and deploying it. Adding a PostgreSQL instance was not problematic too. I read the connection string from the environment, restored a backup and app get run instantly. Based on this experience, I decided to go the same path for a Django/Python project.

It did not work in the first try. Soon, I realized that I should add many stuff to project so Heroku can run my app. Additionally, as it was the first time that this project was going to be hosted in production, I did some work to get it to work. For instance, used dj_database_url for parsing PostgreSQL's connection URL. Then enabled correct CORS policy and and set ALLOWED_HOSTS.

After set the project to run correctly in a production instance, I added some stuff required by Heroku. It included Procfile and requirements.txt and their proper content. While searching online, I found Heroku's formal sample for a bare minimum Django project for Heroku. It helped me a lot. Finally, I saw my project's API run correctly and happily.

My Django/Python had an app which was based on server side rendering. I just tried that part and, oh my god! it was not working! CSS and JS files were not served correctly. I just saw a bunch of 404 errors in the browser's console. Investigating the problem, I realized that it is because of static file not configured correctly. My local had not any problem, But Heroku one showed either 404 error or Heroku build errors. I did about 10 commits so the problem solved.

I have many experiences with ASP.NET/IIS, it was not surprising me if the IIS configuration is hard. But I expected an easier process with Django/Python. By the way, project is now running in Heroku based on PostgreSQL happily!

Discussion (0)

Forem Open with the Forem app