<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ed Morley</title>
    <description>The latest articles on DEV Community by Ed Morley (@edmorley).</description>
    <link>https://dev.to/edmorley</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F433611%2Febe2b2c8-5829-462f-be75-af2643f90990.jpeg</url>
      <title>DEV Community: Ed Morley</title>
      <link>https://dev.to/edmorley</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edmorley"/>
    <language>en</language>
    <item>
      <title>From Project to Productionized with Python</title>
      <dc:creator>Ed Morley</dc:creator>
      <pubDate>Thu, 16 Jul 2020 18:20:20 +0000</pubDate>
      <link>https://dev.to/heroku/from-project-to-productionized-with-python-mcc</link>
      <guid>https://dev.to/heroku/from-project-to-productionized-with-python-mcc</guid>
      <description>&lt;p&gt;We hope that you and your loved ones are staying safe from the COVID-19 pandemic. As a result of its effect on large gatherings, &lt;a href="https://pycon.blogspot.com/2020/03/pycon-us-2020-in-pittsburgh.html"&gt;PyCon 2020 was &lt;del&gt;cancelled&lt;/del&gt; changed to an online event&lt;/a&gt;. Although not being able to gather in person was disheartening for organizers, speakers, and attendees, the Python community shared virtual high-fives and hugs with &lt;a href="https://us.pycon.org/2020/online/"&gt;PyCon US 2020 Online.&lt;/a&gt; We &lt;a href="https://www.youtube.com/watch?v=1923eduj0Gg"&gt;recorded our planned Heroku workshop for the event&lt;/a&gt;, on which this blog post is based.&lt;/p&gt;

&lt;p&gt;Imagine that you've just spent the last two weeks pouring all your energy into an application. It's magnificent, and you're finally ready to share it on the Internet. How do you do it? In this post, we're going to walk through the hands-on process aimed at Python developers deploying their local application to Heroku.&lt;/p&gt;

&lt;p&gt;An application running on Heroku works best as &lt;a href="https://12factor.net/"&gt;a 12-factor application&lt;/a&gt;. This is actually a concept that Heroku championed over 10 years ago. It's the idea that you build an application with robust redeployments in mind. Most of this workshop is actually not specific to Heroku, but rather, about taking a regular Django application and making it meet the 12 factor app methodology, which has become a standard that most cloud deployment providers not only support but recommend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before completing this workshop, we're going to make a few assumptions about you, dear reader. First, this is not going to be a Django tutorial. If you're looking for an introduction to Django, &lt;a href="https://www.djangoproject.com/start/"&gt;their documentation has some excellent tutorials to follow&lt;/a&gt;. You will also need a little bit of &lt;a href="https://git-scm.com"&gt;Git&lt;/a&gt; familiarity, and have it installed on your machine.&lt;/p&gt;

&lt;p&gt;In order to complete this workshop, you'll need a few things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://signup.heroku.com/"&gt;An account on Heroku&lt;/a&gt;. This is completely free and doesn't require any payment information.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://devcenter.heroku.com/articles/heroku-cli#download-and-install"&gt;The Heroku CLI&lt;/a&gt;. Once your application is on Heroku, this will make managing it much easier.&lt;/li&gt;
&lt;li&gt;You'll need to &lt;a href="https://github.com/heroku-python/PyCon2020"&gt;clone the repository for this workship&lt;/a&gt;, and be able to open it in a text editor.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With all that sorted, it's time to begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  Look around you
&lt;/h2&gt;

&lt;p&gt;With the project cloned and available on your computer, take a moment to explore its structure. We'll be modifying the &lt;code&gt;manage.py&lt;/code&gt; and &lt;code&gt;requirements.txt&lt;/code&gt; files, as well as &lt;code&gt;settings.py&lt;/code&gt; and &lt;code&gt;wsgi.py&lt;/code&gt; in the &lt;code&gt;gettingstarted&lt;/code&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating &lt;code&gt;.gitignore&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;To begin with, we'll be updating the gitignore file. &lt;a href="https://git-scm.com/docs/gitignore"&gt;A gitignore file excludes files&lt;/a&gt; which you don't want to check into your repository. In order to deploy to Heroku, you don't technically need a gitignore file. You can deploy successfully without one, but it's highly recommended to always have one (and not just for Heroku). A gitignore can be essential for keeping out passwords and credentials keys, large binary files, local configurations, or anything else that you don't want to expose to the public.&lt;/p&gt;

&lt;p&gt;Copy the following block of code and paste it into the gitignore file in the root of your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/venv
__pycache__
db.sqlite3 # not needed if you're using Postgres locally
gettingstarted/static/

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;venv&lt;/code&gt; directory contains &lt;a href="https://docs.python.org/3/tutorial/venv.html"&gt;a virtual environment&lt;/a&gt; with the packages necessary for your local Python version. Similarly, the &lt;code&gt;__pycache__&lt;/code&gt; directory contains &lt;a href="https://docs.python.org/3/tutorial/modules.html#compiled-python-files"&gt;precompiled modules unique to your system&lt;/a&gt;. We don't want to check in our database (&lt;code&gt;db.sqlite3&lt;/code&gt;), as we don't want to expose any local data. Last, the static files will be automatically generated for us during the build and deploy process to Heroku, so we'll exclude the &lt;code&gt;gettingstarted/static/&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Go ahead and run &lt;code&gt;git status&lt;/code&gt; on your terminal to make sure that gitignore is the only file that's been modified. After that, call &lt;code&gt;git add&lt;/code&gt;, then &lt;code&gt;git commit -m "step 1 add git ignore"&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modularize your settings
&lt;/h3&gt;

&lt;p&gt;Next up, we want to modularize our Django settings. To do that, add a new folder within &lt;code&gt;gettingstarted&lt;/code&gt; called &lt;code&gt;settings&lt;/code&gt;. Then, move the &lt;code&gt;settings.py&lt;/code&gt; file into that directory. Since this naming scheme is a bit confusing, let's go ahead and rename that file to &lt;code&gt;base.py&lt;/code&gt;. We'll call it that because it will serve as the base (or default) configuration that all the other configurations are going to pull from. If something like &lt;code&gt;dev.py&lt;/code&gt; or &lt;code&gt;local.py&lt;/code&gt; makes more sense to you, feel free to use that instead!&lt;/p&gt;

&lt;p&gt;Local projects only have one environment to keep track of: your local machine. But once you want to deploy to different places, it's important to keep track of what settings go where. Nesting our settings files this way makes it easy for us to keep track of where those settings are, as well as take advantage of Heroku's continuous delivery tool pipelines.&lt;/p&gt;

&lt;p&gt;By moving and renaming the settings file, our Django application now has two broken references. Let's fix them before we move on.&lt;/p&gt;

&lt;p&gt;The first is in the &lt;code&gt;wsgi.py&lt;/code&gt; in your &lt;code&gt;gettingstarted&lt;/code&gt; folder. Open it up, and on &lt;a href="https://github.com/heroku-python/PyCon2020/blob/1a4cf7eabfcc994f60f4b8efeed1f0d9a245e768/gettingstarted/wsgi.py#L12"&gt;line 12&lt;/a&gt; you'll see that a default Django settings module is being set to &lt;code&gt;gettingstarted.settings&lt;/code&gt;, a file which no longer exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setdefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DJANGO_SETTINGS_MODULE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gettingstarted.settings&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fix this, append the name of the file you just created in the settings subfolder. For example, since we called ours &lt;code&gt;base.py&lt;/code&gt;, the line should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setdefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DJANGO_SETTINGS_MODULE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gettingstarted.settings.base&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After saving that, navigate up one directory to &lt;code&gt;manage.py&lt;/code&gt;. On &lt;a href="https://github.com/heroku-python/PyCon2020/blob/1a4cf7eabfcc994f60f4b8efeed1f0d9a245e768/manage.py#L6"&gt;line 6&lt;/a&gt;, you'll see the same default being set for the Django settings module. Once again, append &lt;code&gt;.base&lt;/code&gt; to the end of this line, then commit both of them to Git.&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous delivery pipelines
&lt;/h2&gt;

&lt;p&gt;In an application's deployment lifecycle, there are typically four stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You build your app in the development stage on your local machine to make sure it works.&lt;/li&gt;
&lt;li&gt;Next comes the review stage, where you check to see if your changes pass with the full test suite of your code base.&lt;/li&gt;
&lt;li&gt;If that goes well, you merge your changes to staging. This is where you have conditions as close to public as possible, perhaps with some dummy data available, in order to more accurately predict how the change will impact your users.&lt;/li&gt;
&lt;li&gt;Lastly, if all that goes well, you push to production, where the change is now live for your customers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Continuous delivery (CD) workflows are designed to test your change in conditions progressively closer and closer to production and with more and more detail. Continuous delivery is a powerful workflow that can make all of the difference in your experience as a developer once you've productionized your application. Heroku can save you a lot of time here, as we've already built the tools for you to have a continuous delivery workflow. From your dashboard on Heroku, you can—with the mere click of a button!–&lt;a href="https://devcenter.heroku.com/articles/pipelines"&gt;set up a pipeline&lt;/a&gt;, add applications to staging and production, and deploy them.&lt;/p&gt;

&lt;p&gt;If you &lt;a href="https://devcenter.heroku.com/articles/github-integration"&gt;connect your GitHub repository&lt;/a&gt;, pipelines can also automatically deploy and test new PRs opened on your repo. By providing the tooling and automating these processes, Heroku's continuous delivery workflow is powerful enough to help you keep up with your development cycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding new middleware to &lt;code&gt;base.py&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Modularizing your Django settings is a great way to take advantage of this continuous delivery workflow by splitting up your settings, whether you're deploying to Heroku or elsewhere, but there's one more change we have to make to &lt;code&gt;base.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Django static assets work best when you also use the &lt;a href="http://whitenoise.evans.io/en/stable/django.html"&gt;whitenoise&lt;/a&gt; package to manage your static assets. It's really easy to add to your project.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;base.py&lt;/code&gt; file, scroll down to about line 43, and you should see an array of package names like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;MIDDLEWARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django.middleware.security.SecurityMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;# Whitenoise goes here
&lt;/span&gt;    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django.contrib.sessions.middleware.SessionMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django.middleware.common.CommonMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django.middleware.csrf.CsrfViewMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django.contrib.auth.middleware.AuthenticationMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django.contrib.messages.middleware.MessageMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django.middleware.clickjacking.XFrameOptionsMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is your list of &lt;a href="https://docs.djangoproject.com/en/3.0/topics/http/middleware/"&gt;Django middleware&lt;/a&gt;, which are sort of like plugins for your server. Django loads your middleware in the order that it's listed, so you always want your security middleware first, but it's important to add whitenoise as the second step in this base file.&lt;/p&gt;

&lt;p&gt;Copy the following line of code and replace the line that says &lt;code&gt;Whitenoise goes here&lt;/code&gt; with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;whitenoise.middleware.WhiteNoiseMiddleware&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've loaded whitenoise as middleware, but to actually &lt;em&gt;use&lt;/em&gt; the whitenoise compression, we need to set one more variable. Copy the following code and paste it right at the end of your &lt;code&gt;base.py&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;STATICFILES_STORAGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;whitenoise.storage.CompressedManifestStaticFilesStorage&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, we're done with &lt;code&gt;base.py&lt;/code&gt;. Congratulations! Save your work and commit it to Git.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up &lt;code&gt;heroku.py&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Our base settings are complete, but now we need our Heroku-specific settings. Create a new file under &lt;code&gt;gettingstarted/settings&lt;/code&gt; called &lt;code&gt;heroku.py&lt;/code&gt; and paste the following block of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
Production Settings for Heroku
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;environ&lt;/span&gt;

&lt;span class="c1"&gt;# If using in your own project, update the project namespace below
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;gettingstarted.settings.base&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;

&lt;span class="n"&gt;env&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;# set casting, default value
&lt;/span&gt;    &lt;span class="n"&gt;DEBUG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# False if not in os.environ
&lt;/span&gt;&lt;span class="n"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;DEBUG&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Raises django's ImproperlyConfigured exception if SECRET_KEY not in os.environ
&lt;/span&gt;&lt;span class="n"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;SECRET_KEY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;ALLOWED_HOSTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ALLOWED_HOSTS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Parse database connection url strings like psql://user:pass@127.0.0.1:8458/db
&lt;/span&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;# read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
&lt;/span&gt;    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;db&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see in this file the values that we're listing here are the ones that we're overriding from our base settings, so these are the settings that will be different and unique for Heroku.&lt;/p&gt;

&lt;p&gt;To do this, we're using one of my favorite packages, &lt;a href="https://github.com/joke2k/django-environ"&gt;Django-environ&lt;/a&gt;. This allows us to quickly and easily interface with the operating system environment without knowing much about it. It has built-in type conversions, and in particular it has automatic database parsing. This is all we need in order to parse our Heroku Postgres database URL that we will be given. It's just really convenient.&lt;/p&gt;

&lt;h2&gt;
  
  
  Heroku-specific files
&lt;/h2&gt;

&lt;p&gt;That's all the work we need to do to get our application into 12 factored shape, but there are three more files we need in order to deploy to Heroku.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;requirements.txt&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In addition to the packages your project already uses, there are a few more you need to deploy to Heroku. If we take a look at the provided &lt;code&gt;requirements.txt&lt;/code&gt; file, you can see these required packages here. We've already talked about Django, Django-environ, and whitenoise, and we've already configured those for use. But the other two are also important and needed for deployment.&lt;/p&gt;

&lt;p&gt;The first one is called &lt;a href="https://gunicorn.org/"&gt;Gunicorn&lt;/a&gt;. This is the recommended WSGI server for Heroku. We'll take a look at configuring this in just a bit. The next one is &lt;a href="https://www.psycopg.org/"&gt;psychopg2&lt;/a&gt;. This is a Postgres database adapter. You need it in your &lt;code&gt;requirements.txt&lt;/code&gt; file to deploy, but you don't need any code changes in order to activate it.&lt;/p&gt;

&lt;p&gt;A quick side note: we're keeping our discussion on packages simple for the purpose of this demo, but when you're ready to deploy a real project to Heroku, consider freezing your dependencies. You can do this with the &lt;a href="https://pip.pypa.io/en/stable/reference/pip_freeze/"&gt;&lt;code&gt;pip freeze&lt;/code&gt;&lt;/a&gt; command. This will make your build a little bit more predictable by locking your exact dependency versions into your Git repo. If your dependencies aren't locked, you might find yourself deploying one version of Django one day and a new one the next.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;runtime.txt&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Heroku will install a default Python version if you don't specify one, but if you want to pick your Python version, you'll need a &lt;code&gt;runtime.txt&lt;/code&gt; file. Create one in the root directory, next to your &lt;code&gt;requirements.txt&lt;/code&gt;, &lt;code&gt;manage.py&lt;/code&gt;, &lt;code&gt;.gitignore&lt;/code&gt; and the rest. Specify your Python version with the prefix &lt;code&gt;python-&lt;/code&gt;, followed by the major, minor, and patch version that you want your application to run on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python-3.8.2

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;Procfile&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The last file we need to add is a file specific to Heroku: &lt;a href="https://devcenter.heroku.com/articles/procfile"&gt;the &lt;code&gt;Procfile&lt;/code&gt;&lt;/a&gt;. This is what we use to specify the processes our application should run. The processes specified in this file will automatically boot on deploy to Heroku. Create a file named &lt;code&gt;Procfile&lt;/code&gt; in the root level directory, right next to your &lt;code&gt;requirements.txt&lt;/code&gt; and &lt;code&gt;runtime.txt&lt;/code&gt; files. (Make sure to capitalize the P of Procfile otherwise Heroku might not recognize it!) Copy-paste the following lines into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;release: python3 manage.py migrate
web: gunicorn gettingstarted.wsgi --preload --log-file -

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/release-phase"&gt;The &lt;code&gt;release&lt;/code&gt; phase&lt;/a&gt; of a Heroku deployment is the best place to run tasks, like migrations or updates. The command we will run during this phase is to simply run the &lt;code&gt;migrate&lt;/code&gt; task defined in &lt;code&gt;manage.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The other process is the &lt;code&gt;web&lt;/code&gt; process, which is very important, if not outright essential, for any web application. This is where we pass our Gunicorn config, the same things we need when running the server locally. We pass it our WSGI file, which is located in the &lt;code&gt;gettingstarted&lt;/code&gt; directory, and then we pass a few more flags to add it a bit more configuration. The &lt;code&gt;--preload&lt;/code&gt; flag ensures that the app can receive requests just a little bit faster; the &lt;code&gt;--logfile&lt;/code&gt; just specifies that the log file should get routed to Heroku.&lt;/p&gt;

&lt;h2&gt;
  
  
  Readying for deployment
&lt;/h2&gt;

&lt;p&gt;Take a second before moving on and just double check that you've saved and committed all of your changes to Git. Remember, we need those changes in the Git repo in order for them to successfully deploy. After that, let's get ready to make an app!&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating an app with &lt;code&gt;heroku create&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Since we have the Heroku CLI installed, we can &lt;a href="https://devcenter.heroku.com/articles/creating-apps"&gt;call &lt;code&gt;heroku create&lt;/code&gt; on the command line to have an app generated&lt;/a&gt; for us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku create
Creating app... &lt;span class="k"&gt;done&lt;/span&gt;, ⬢ mystic-wind-83
Created http://mystic-wind-83.herokuapp.com/ | git@heroku.com:mystic-wind-83.git

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your app will be assigned a random name—in this example, it's &lt;code&gt;mystic-wind-83&lt;/code&gt;—as well as a publicly accessible URL.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting environment variables on Heroku
&lt;/h3&gt;

&lt;p&gt;When we created our &lt;code&gt;heroku.py&lt;/code&gt; settings file, we used Django-environ to load environment variables into our settings config. &lt;a href="https://devcenter.heroku.com/articles/config-vars"&gt;Those environment variables also need to be present in our Heroku environment&lt;/a&gt;, so let's set those now.&lt;/p&gt;

&lt;p&gt;The Heroku CLI command we'll be using for this is &lt;code&gt;heroku config:set&lt;/code&gt;. This will take in key-value pairs as arguments and set them in your Heroku runtime environment. First, let's configure our allowed hosts. Type the following line, and replace &lt;code&gt;YOUR_UNIQUE_URL&lt;/code&gt; with the URL generated by &lt;code&gt;heroku create&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku config:set &lt;span class="nv"&gt;ALLOWED_HOSTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;YOUR_UNIQUE_URL&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's set our Django settings module. This is what determines what settings configuration we use on this platform. Instead of using the default of &lt;code&gt;base&lt;/code&gt;, we want the Heroku-specific settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku config:set &lt;span class="nv"&gt;DJANGO_SETTINGS_MODULE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;gettingstarted.settings.heroku

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, we'll need to create a &lt;code&gt;SECRET_KEY&lt;/code&gt;. For this demo, it doesn't matter what its value is. You can use a secure hash generator like &lt;code&gt;md5&lt;/code&gt;, or a password manager's generator. Just be sure to keep this value secure, don't reuse it, and NEVER check it into source code! You can set it using the same CLI command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku config:set &lt;span class="nv"&gt;SECRET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;gobbledygook&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Provisioning our database
&lt;/h2&gt;

&lt;p&gt;Locally, Django is configured to use a SQLite database but we're productionizing. We need something a little bit more robust. Let's provision a Postgres database for production.&lt;/p&gt;

&lt;p&gt;First, let's check if we have a database already. The &lt;a href="https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-addons-all-app-app"&gt;&lt;code&gt;heroku addons&lt;/code&gt;&lt;/a&gt; command will tell us if one exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku addons
No add-ons &lt;span class="k"&gt;for &lt;/span&gt;app mystic-wind-83.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No add-ons exist for our app, which makes sense—we just created it! To add a Postgres database, we can use the &lt;code&gt;addons:create&lt;/code&gt; command like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku addons:create heroku-postgresql:hobby-dev

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Heroku offers several tiers of Postgres databases. &lt;code&gt;hobby-dev&lt;/code&gt; is the free tier, so you can play around with this without paying a dime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Going live
&lt;/h2&gt;

&lt;p&gt;It is time. Your code is ready, your Heroku app is configured, you are ready to deploy. This is the easy part!&lt;/p&gt;

&lt;p&gt;Just type out&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git push heroku master

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we'll take care of the rest! You'll see your build logs scrolling through your terminal. This will show you what we're installing on your behalf and where you are in the build process. You'll also see the &lt;code&gt;release&lt;/code&gt; phase as well that we specified earlier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scaling up
&lt;/h2&gt;

&lt;p&gt;The last step is to scale up our web process. This creates new dynos, or, in other words, copies of your code on Heroku servers to handle more web traffic. You can do this using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku ps:scale &lt;span class="nv"&gt;web&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see your app online, enter &lt;code&gt;heroku open&lt;/code&gt; on the terminal. This should pop open a web browser with the site you just built.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debugging
&lt;/h2&gt;

&lt;p&gt;If you hit some snags, don't worry, we have some tips that might help:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are all of your changes saved and checked into Git?&lt;/li&gt;
&lt;li&gt;Are your changes on the &lt;code&gt;master&lt;/code&gt; branch or are they on a different branch? Make sure that whatever you're deploying, all of your changes are in that Git branch.&lt;/li&gt;
&lt;li&gt;Did you deploy from the root directory of your project? Did you also call &lt;code&gt;heroku create&lt;/code&gt; from the root directory of your project? If not, this could absolutely cause a trip up.&lt;/li&gt;
&lt;li&gt;Did you remove anything from the code in the provided demo that we didn't discuss?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Logging
&lt;/h3&gt;

&lt;p&gt;If you've run through this list and still have issues, take a look at your log files. In addition to your build logs—which will tell you whether your application successfully deployed or not—you have access to all logs produced by Heroku and by your application. You can get to these through a couple of different ways, but the quickest way is just to run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;heroku logs &lt;span class="nt"&gt;--tail&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Remote console
&lt;/h3&gt;

&lt;p&gt;Another tool you have is the &lt;code&gt;heroku run bash&lt;/code&gt; command. This provides you with direct access from your terminal to a Heroku dyno with your code deployed to it. If you type &lt;code&gt;ls&lt;/code&gt;, you can see that this is your deployed application. It can be useful to check that what is up here matches what is locally on your machine. If not, you might see some issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Congratulations on successfully deploying your productionized app onto Heroku!&lt;/p&gt;

&lt;p&gt;To help you learn about Heroku, we also have a wealth of technical documentation. Our &lt;a href="https://devcenter.heroku.com"&gt;Dev Center&lt;/a&gt; is where you'll find most of our technical how-to and supported technologies information. If you're having a technical issue, chances are someone else has asked the same question and it's been answered on our help docs. Use these resources to solve your problems as well as to learn about best practices when deploying to Heroku.&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
