📕
SEIRFX
  • Introduction
  • About These Notes
  • Schedule
  • Unit 2
    • Node
      • Internet Fundamentals
      • Full-Stack Fundamentals
      • Intro to Node
      • Node Modules
      • Node Packages
    • Express
      • Intro to Express
      • Routes
      • Routes Lab
      • Views
      • Templates
      • Layouts & Controllers
    • CRUD & REST
      • GET & POST
      • GET & POST Lab
      • PUT & DELETE
    • API Calls in Express
      • Axios
      • Request (no longer maintained)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
  • Development Workflow
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
      • Objects Lesson
      • Objects quick guide
      • Object-ception
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Scope
      • Callbacks
      • Higher Order Functions
      • Callbacks Review Lab
      • Timing Functions
      • Iterators
      • Combining Data Types
      • Combining Data Types Lab
    • Javascript in the browser
      • DOM and Events
      • DOM Manipulation
      • DOM Review
      • DOM Review Lab
      • HP DOM Lab
      • Programmatic DOM Manipulation
      • Grids & Pyramids
      • DOM & Data
      • DOM Events
      • Color Palette Picker
      • Sketchpad
    • HTML5 Canvas
    • How To Reduce Redundancy
    • OOP
      • Westworld Lab
      • OOP Factories
      • OOP Inheritance
      • OOP Inheritance Lab
      • Tomagotchi Lab
      • OOP Space Battle
      • OOP Snowman
      • (2019) JavaScript OOP
      • (2016) OOP with Classes
      • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Left over Node/Express
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Being Successful in SEI
    • Internet Fundamentals
      • Internet Lab
    • Adjacent Workflow
      • UX/UI
      • Wireframing Exercise: Build an Idea
      • Agile
    • Post SEI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Pipenv
  • Get your Pipfile
  • What goes in the Pipfile?
  • Gunicorn
  • Procfile
  • Static Assets and Whitenoise
  • Set up Database Settings
  • Splitting up settings
  • Lock that Pipfile
  • Deploy!
  • Before Deploying...
  • Create Heroku App
  • Config variables
  • Provision your database
  • Push the code!
  • Troubleshooting
  • Resources

Was this helpful?

  1. Deployment and Config

Deploy - Django

We'll walk through deploying your Django app on Heroku. Order of operations will be as follows:

  • Install pipenv

  • Set up Pipfile

  • Install gunicorn

  • Create Procfile

  • Static Assets & Whitenoise

  • Database settings / Split settings

  • Generate Lockfile, Pipfile.lock

  • Deploy to Heroku

    • Create App

    • Set Config Vars

    • Provision Database

    • Push code

    • Collectstatic for Static Assets

Pipenv

Install pipenv via your terminal.

pip install pipenv

Protip: Pipenv is a package that will help us manage Pipfiles. We can also install packages with pipenv, which will be similar to how regular pip install works, but it also automatically adds the package to the Pipfile.

Get your Pipfile

Your Pipfile is a list of dependencies for your Django app. It replaces the old way of keeping track of this, where you would use a freeze command to create a requirements.txt file. Now instead we will have Pipfile and Pipfile.lock. We'll cover the lock file in a moment. For now, let's set up your Pipfile.

Tip: You may see a lot of code on the internet that still uses requirements.txt. Remember, Pipfiles are a reaplcement for this. We do not need a requirements.txt!

What goes in the Pipfile?

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true

[packages]

[requires]
python_version = "3.6"

This is a pretty good start. The only section we really need to put any thinking into is the [packages] section. Here, we just need to include any packages we actually import and use. This means, don't worry about sub-dependencies as they will be installed automatically!

Gunicorn

Gunicorn is a server that supports concurrent processes. It will make your Heroku app feel faster and be better utilized.

pipenv install gunicorn

Procfile

Now that we have Gunicorn installed, let's use it! Create a file called Procfile in the top level folder of you app.

Remember the capital P in Procfile!

Your Procfile should look like the following:

web: gunicorn yourappname.wsgi
release: python manage.py migrate

Obviously, replace yourappname with your actual app name. The release command will do any database migrations that are needed upon deployment.

Static Assets and Whitenoise

Unfortunately, Django makes you jump through some hoops to get static files working in the production environment.

After you're done with that, run the following commnd to collect your assets into the static folder:

heroku run python manage.py collectstatic

Set up Database Settings

There's no one way to do this, but you'll eventually find that you need different settings between a development environment and a production environment. We'll need to set up our database settings in settings.py to be configured for our production environment. These settings will be different than our development environment settings. There are a couple ways to go about this.

First option is that you can comment/uncomment the production code right before you push each time. This can get tedious after awhile.

The second option is to make separate settings.py files for development and production environments.

The third option is to make an environment variable that you check in your settings.py file and load things you need within conditional statements.

The fourth option is to use a third party tool that handles it for you.

NOTE: In a professional setting, you'll have a test environment (and possibly a staging environment) too! Test environments typically mirror production and act as a place you can test things outside of your local machine so that code doesn't go straight from development to production.

Splitting up settings

Lock that Pipfile

We'll use pipenv to lock up our Pipfile, creating a file called Pipfile.lock. You will want to relock your Pipfile any time you make changes to it. Regenerate your lock file with the following command:

pipenv lock

Warning: Never change your Pipfile.lock! It is always to be generated and never to be touched!

Deploy!

Before Deploying...

At this point in the process, the Pipfile looks something like this

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true

[packages]
django = "*"
gunicorn = "*"
django-heroku = "*"
django-cors-headers = "*"
dj-database-url = "*"

[requires]
python_version = "3.6.5"

Make sure you've locked the latest version of the Pipfile before pushing to Heroku!

pipenv lock

Create Heroku App

You can use the heroku CLI to create an app in your terminal. This will automatically hook up the remote correctly from the folder you run the command in (make sure you're in the root/top-level!).

heroku apps:create yourappnamehere

Config variables

Remember to add any config variables!

heroku config:set MY_ENV_VAR=123

You can also do this in the Heroku dashboard in your web browser if you wish! If you didn't use any environment variables, you can skip this step. Alternatively, if you forget this step or end up adding variables later, you can do this step again any time as it is not a part of the deployment process!

Provision your database

Heroku uses an addon to support Postgres. You can add it with the following command:

heroku addons:create heroku-postgresql:hobby-dev

Push the code!

Once that's done, go ahead and push to Heroku using git.

git add .
git commit -m "Deploying Django App"
git push heroku master

This will trigger a build. The first build will take the longest. After that, it will start to cache some of your packages to make it faster.

NOTE: The caching behavior can be a blessing or a curse. While going faster is great a majority of the time, if you somehow get the wrong version of a package installed, you may have a difficult or impossible time trying to get it to wipe that version from its cache.

Troubleshooting

As normal, check your heroku logs to see what errors have come up. Logs will also contain any print statements you've left in your code (which you definitely should take out once you've debugged!) You can access the logs by typing heroku logs in your terminal while in the folder containing your Django app (top level).

Resources

PreviousDeploy - S3 Static SitesNextDeploy - Flask

Last updated 4 years ago

Was this helpful?

According to the , when you run the pipenv --python 3 command, if you don't have a Pipfile already, one will be generated for youthat looks like this:

The command above both installs gunicorn and adds it to the Pipfile. If after your app is up and running, you are experiencing performance issues, take a read through and configure WEB_CONCURRENCY and/or preload settings accordingly.

You can follow the directions on to get it set up.

This website has . Enjoy!

- this includes an explanation about why Pipfile is superior to requirements.txt

- this includes more info on default settings

- more explanations of best practices in less technial language

- Official docs on settings.py

- A helpful tool that explains how to set up your settings.py in layman's terms!

Pipenv docs
Heroku's Docs regarding Gunicorn
their docs
directions and code examples for all three options
Pipfile Docs
Pipenv Docs
Pipenv Guide
Heroku's Docs regarding Gunicorn
Provisioning Postgres DB on Heroku
Django-configurations
Configure Django Settings
Django Whitenoise Docs