Django Hello, World + Fly.io Deployment
In this tutorial we will build a "Hello, World" website with Django and then deploy it to Fly.io.
If you want to learn Django properly, I provide step-by-step instructions and detailed explanations in my book Django for Beginners. The first four chapters are available free online.
Initial Set Up
Open a new command line shell. The code can live anywhere on your computer. We'll put it on the desktop in a folder called helloworld
.
# Windows $ cd onedrive\desktop\code $ mkdir helloworld $ cd helloworld # macOS $ cd ~/desktop/code $ mkdir helloworld && cd helloworld
Create a new virtual environment called .venv
, activate it, and install Django with Pip.
# Windows $ python -m venv .venv $ .venv\Scripts\Activate.ps1 (.venv) $ python -m pip install django~=4.1.0 # macOS $ python3 -m venv .venv $ source .venv/bin/activate (.venv) $ python3 -m pip install django~=4.1.0
Use the startproject
command to make a new Django project called demo
and a new app called pages
.
(.venv) $ django-admin startproject demo . (.venv) $ python manage.py startapp pages
Add the new pages
app to the INSTALLED_APPS
configuration.
# demo/settings.py INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", "pages", # new ]
Then run migrate
to set up the initial database and runserver
to start the local Django web server.
(.venv) $ python manage.py migrate (.venv) $ python manage.py runserver
Open up http://127.0.0.1:8000/
in your web browser to see the Django welcome page.
Django Hello, World
Now let's configure a basic view that returns the text "Hello, World!".
# pages/views.py from django.http import HttpResponse def homePageView(request): return HttpResponse("Hello, World!")
Create a new file called pages/urls.py
with the following code.
# pages/urls.py from django.urls import path from .views import homePageView urlpatterns = [ path("", homePageView, name="home"), ]
And update the project-level demo/urls.py
file as well.
# demo/urls.py from django.contrib import admin from django.urls import path, include # new urlpatterns = [ path("admin/", admin.site.urls), path("", include("pages.urls")), # new ]
We're done! Start the local server again:
(.venv) $ python manage.py runserver
If you refresh the browser for http://127.0.0.1:8000/
it now displays the text "Hello, World!"
Django Deployment Checklist
Django is configured by default for local development. A proper production-ready deployment is quite involved--see How to Deploy Django and Django deployment checklist--but the following insecure steps will let us deploy our simple site for demonstration purposes.
First, update ALLOWED_HOSTS
to accept all hosts.
# demo/settings.py ALLOWED_HOSTS = ["*"]
Second, install Gunicorn as our production server.
(.venv) $ python -m pip install gunicorn==20.1.0
Then create a requirements.txt
file listing the packages in our Python virtual environment.
(.venv) $ pip freeze > requirements.txt
This creates a new requirements.txt
file. If you look inside it there should be at least the following four packages:
asgiref==3.5.2 Django==4.1.3 gunicorn==20.1.0 sqlparse==0.4.3
Fly Deployment
Time for Fly.io deployment. Fly 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.
Fly.io requires a credit card on file now. Deployment employs real costs on hosting companies so it is a reasonable request. It also helps them crack down on fraud which is a major issue for all hosting companies with free tiers. Pricing is based on usage and quite reasonable especially compared to its equivalent on Heroku.
To configure and launch the app, run the fly launch
command and follow the wizard. You can set a name for the app, choose a default region, and choose whether to add a Postgresql database and Upstash Redis database as well.
(.venv) $ fly launch
Creating app in ~/django-hello-fly Scanning source code Detected a Django app ? Choose an app name (leave blank to generate one): django-hello-fly automatically selected personal organization: Will Vincent ? Choose a region for deployment: Ashburn, Virginia (US) (iad) Created app django-hello-fly in organization personal Set secrets on django-hello-fly: SECRET_KEY Wrote config file fly.toml ? Would you like to set up a Postgresql database now? No ? Would you like to set up an Upstash Redis database now? No Your app is ready! Deploy with `flyctl deploy`
This creates two new files in the project that are automatically configured: a Dockerfile and fly.toml
file to configure applications for deployment.
We do not have static files in this example so comment out that line near the bottom of the autogenerated Dockerfile
.
...
# RUN python manage.py collectstatic --noinput
...
To deploy the application use the following command:
(.venv) $ fly deploy
This will take a few seconds as it uploads your application, verifies the app configuration, builds the image, and then monitors to ensure it starts successfully. Once complete visit your app with the following command:
(.venv) $ fly open
You are up and running! Wasn't that easy? Mine is located at https://django-hello-world-fly.fly.dev/.
Conclusion
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
Dockerfile
image. - The
fly.toml
file controls your app configuration and can be modified as needed. fly dashboard
can 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 usingfly
commands. Tryfly help
to see what you can do.
If you want to take the next step learn how to deploy a production-ready Django application with a PostgreSQL database to Fly.io, check out this follow-up tutorial.