Deploying my Django + React Boilerplate to Heroku
Tags: django, python, devops
Published: 2020-03-15 10:25:43

<!-- wp:paragraph -->
<p>Recently, I developed a new <a href="https://github.com/marcphilippebeaujean-abertay/django-react-boilerplate">boilerplate</a> for Django apps with React. A boilerplate refers to a project that can be used as a base that you start from, allowing you to skip a lot of configuration. The project I developed has the following pre-built features:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Project configuration that easily lets you toggle between production and local development mode</li><li>Integration with powerful React modules (styled components, react bootstrap, redux)</li><li>Use of <a href="https://www.typescriptlang.org/">TypeScript</a> to allow for a more maintainable code base</li><li>Includes implementations for the most common user access flows that most apps need (sign up, password reset, login, sessions with tokens)</li><li>Page layout with common components that most apps use (cookie banner, footer, nav bar, loading bar)</li><li>Out-of-the-box instant CI/CD with Heroku</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>In this tutorial, I am going to cover how you can deploy this boilerplate to Heroku very easily.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Setting up the Project</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you don't already have an account, head down to the <a href="https://signup.heroku.com/login">sign up</a>. We then want to create a new app.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":591,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/03/django-react-tut-1.png" alt="" class="wp-image-591"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Go to the Deploy section on the App dashboard and choose GitHub as the Deployment method. You can then setup Heroku to sync with your GitHub repository - this is the simplest method in my opinion, because you don't need to install the Heroku CLI and the boilerplate is designed to work without it. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":592,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/03/image.png" alt="" class="wp-image-592"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Adding a Postgres Database</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>For this tutorial, I will be using a postgres database, as the boilerplate is laid about by default to use it. Heroku lets you use a free instance of postgres, simply go to the resources tab and search for it in the search bar.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":593,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/03/image-1-1024x396.png" alt="" class="wp-image-593"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Heroku will configure the database for us, so we need to store the credentials and inject them into our app. Click on your postgres add-on and you will be taken to a new dashboard for your database. Hit the Settings tab and click on view credentials.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":594,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/03/image-2-1024x117.png" alt="" class="wp-image-594"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now, you should see all the information, for your database. Take a screenshot or keep it open in a separate tab.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":595,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/03/django-react-boilerplate-4-1024x440.png" alt="" class="wp-image-595"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Setting up Environment Variables</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The boilerplate project uses so-called environment variables that are injected into the project when you deploy in production mode.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":596,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/03/image-3.png" alt="" class="wp-image-596"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The os.environ.get method is used to grab any variables from the surrounding system. Heroku lets us define these, as those any typical operating system or hosting platform. Important is that we use the same key names as are in the code. You will also see from the code that the boilerplate is using amazon SES configuration to send emails - <a href="https://byteschool.io/post/sending-email-with-smtp-on-aws-and-django">I have a tutorial</a> on this if you are not sure how to set it up or you can replace that code with any email provider of your choosing. Same thing applies to the type of database, as you can see it is configured to Postgres here. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To configure environment variables in Heroku, go back to the Settings tab of your app. You will see a Config Vars option - click Reveal Config Vars. We can now add the environment variables that we need, this is where we enter the database information that Heroku gave us earlier.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":598,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/03/image-4-1024x727.png" alt="" class="wp-image-598"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>...and that should be it! You can go to the Deploy tab, start deployment and enjoy the magic.</p>
<!-- /wp:paragraph -->