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:
Prerequisites:
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.
heroku create <app-name>
.git push heroku master
.If you own a domain name and wish to use it, follow these steps:
heroku domains:add www.example.com
.www.example.com CNAME <app-name>.herokuapp.com
.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
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
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.
Solutions:
psycopg2-binary
to your requirements.txt
and configure your database in your settings.py
.