Deploying a Flask/Django Application

Tutorial 5 of 5

Introduction

This tutorial aims to teach you how to deploy your Flask/Django application to a web server. By the end of this tutorial, you will have a clear understanding of the deployment process and you'll be able to host your Flask or Django app online for users to access.

What you will learn:

  • Various deployment options
  • Configuring a web server
  • Setting up a domain name

Prerequisites:

  • Basic knowledge of Python
  • Familiarity with Flask or Django web framework
  • Basic understanding of web servers

Step-by-Step Guide

Deployment Options

There are many options to deploy your Flask/Django application including Heroku, AWS, Google Cloud Platform, and DigitalOcean. For this tutorial, we will use Heroku due to its simplicity for beginners.

Configuring a Web Server

  1. Start by installing the Heroku CLI on your local machine and log in to your Heroku account.
  2. Navigate to your project directory and create a new Heroku app: heroku create <app-name>.
  3. Commit your changes (if any) and push your code to Heroku: git push heroku master.

Setting Up a Domain Name

If you own a domain name and wish to use it, follow these steps:

  1. Add your domain to your Heroku app: heroku domains:add www.example.com.
  2. Add a DNS record in your domain provider's management console to point to your Heroku app: www.example.com CNAME <app-name>.herokuapp.com.

Code Examples

Deploying Flask App to Heroku

Here's an example of a simple Flask app:

# app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, World!"

if __name__ == '__main__':
    app.run()

Make sure you have a requirements.txt file that lists all your Python dependencies, and a Procfile that tells Heroku how to run your application:

# requirements.txt
flask

# Procfile
web: python app.py

Push your code to Heroku:

git add .
git commit -m "Initial commit"
git push heroku master

Deploying Django App to Heroku

For Django, you will need a requirements.txt file, a Procfile, and a runtime.txt file to specify your Python version:

# requirements.txt
Django
gunicorn

# Procfile
web: gunicorn myproject.wsgi

# runtime.txt
python-3.8.6

Summary

We've covered how to deploy a Flask/Django application, configure a web server using Heroku, and set up a domain name. Next steps include exploring other deployment options and learning how to handle environment variables and database connections in your deployed apps.

Practice Exercises

  1. Deploy a simple Flask application to Heroku.
  2. Deploy a Django application with a Postgres database to Heroku.
  3. Set up a custom domain name for your Heroku app.

Solutions:

  1. Follow the steps in the "Code Examples" section above.
  2. This is similar to the Django example above, but you will need to add psycopg2-binary to your requirements.txt and configure your database in your settings.py.
  3. Purchase a domain from a provider like GoDaddy or Namecheap, then follow the steps in the "Setting Up a Domain Name" section above.