
Initial Local Setup
Make sure that Python is already installed on your computer along with a way to create virtual environments. This allows you to run your project locally, and test that it works, before deploying it to Fly.io.We recommend the latest supported versions of Python.Create a folder for your project. Here we’ll call it
hello-django. Enter the folder with cd:
Virtual Environment
For this guide, we use venv but any of the other popular choices such as Poetry, Pipenv, or pyenv work too.From this point on, the commands won’t be displayed with
(.venv) but we assume you have your Python virtual environment activated.Install Django
With your virtual environment activated, install the latest version of Django using pip:Create a Django Project
Inside thehello-django folder, create a Django project named hello_django:
Don’t forget theNote that by convention, we name Django projects using.at the end. It’s crucial because it tells the script to create the Django project directory structure in the current directory, our folderhello-django.
snake_case: words written in lowercase with spaces replaced by underscore (_). Hyphens (-) are not valid identifiers and you might get this error message:
Create and Map a URL to a View
Now let’s configure a basic view that returns the text,Hello, Fly! by updating the
hello_django/urls.py file:
Run migrations
As part of Django’s core functionality, some existing apps are included by default to provide you with out-of-the-box features. Some of those apps require their own database tables. To initialize the local database and set up those tables, run themigrate command:
Start the Web Server
Nowrunserver to start up Django’s local web server:
http://127.0.0.1:8000/ in your web browser it now displays the text Hello, Fly!.
Django Deployment Checklist
By default, Django is configured for local development. The How to Deploy Django and Django Deployment Checklist guide list the various steps required for a secure deployment.You can also find a complete guide Deploying Django to Production in our Django Beats Blog.However, for demonstration purposes, we can take some shortcuts. First, in the
hello_django/settings.py file update the ALLOWED_HOSTS configuration to accept
a host on which it’s deployed. Use the FLY_APP_NAME
environment variable for that:
requirements.txt file listing all the packages in the current Python virtual environment:
flyctl
Fly.io has its own command-line utility for managing apps, flyctl. If not already installed, follow the instructions on the installation guide and log in to Fly.io.Configure and Deploy your Fly App
To configure and launch the app, run thefly launch command and follow the wizard. You can set a name for the app and choose your primary region. You can also choose to launch and attach a Postgres database and/or a Redis database though we are not using either in this example.
Dockerfile and fly.toml file to configure applications for deployment.
To deploy the application use the following command:
Recap
We started with an empty directory and in a matter of minutes had a running Django application deployed to the web. A few things to note:- Your application is running on a Virtual Machine that was created based on the
Dockerfileimage. - The
fly.tomlfile controls your app configuration and can be modified as needed. fly dashboardcan be used to monitor and adjust your application. Pretty much anything you can do from the browser window you can also do from the command line usingflycommands. Tryfly helpto see what you can do.