Python Django App on Section
Learn how to run a Python Django app at the edge for low latency and high availability. You can use our repo as a template, or perform the steps yourself using the Kubernetes dashboard or kubectl commands.
What You'll Build
Visit https://muddy-butterfly-8163.section.app to see what you'll be building.
Option 1 - Copy Our GitHub Repo
Make a new repo from our template: in your browser visit https://github.com/section/python-django-tutorial and select
Use this template (don't clone, don't fork, but use the template). Choose yourself as an owner, give it a name of your choice, and make it be Public (not Private).
- In your new GitHub repo, under Settings > Secrets > Actions, use
New repository secretto add these two:
SECTION_K8S_API_URL: this is the Kubernetes API endpoint for your new project
SECTION_API_TOKEN: this is a Section API token
- Make any change to the files within
./my_django_app/and watch your changes go live.
Option 2 - Step by Step
Following are step-by-step instructions to deploy a Python Django application to the edge on Section. We'll Dockerize it, and deploy it on Section.
- You need Docker installed so that you can build a docker image, Python installed so you can test it locally (which comes with
pipthe Python package manager since Python 3.4).
Create the Python Django App
Create a new directory for your app.
Initialize the Django application with the following commands:
pip install django
django-admin startproject my_django_app
Run the Django application locally with the following commands:
python manage.py runserver 8080
Now navigate to
http://localhost:8080 in your browser to see the Django app running.
After deploying on Section and using your own domain name(s) you will need to edit the
ALLOWED_HOSTS setting in
my_django_app/settings.py to include your domain name(s). For example, if your domain name is
example.com, you would add the following line to
ALLOWED_HOSTS = ['example.com']
Let's build the container image that we'll deploy to Section. First make a
Dockerfile in your directory with the following content.
RUN pip install django
COPY . /my-django-app
CMD ["manage.py", "runserver", "0.0.0.0:8080"]
Build and tag it.
docker build . -t ghcr.io/YOUR_GITHUB_USERNAME/my-django-app:prod
Launch it locally to test it.
docker run -p 8080:8080 ghcr.io/YOUR_GITHUB_USERNAME/my-django-app:prod
Push it to GitHub Packages. This makes it available to Section.
docker push ghcr.io/YOUR_GITHUB_USERNAME/my-django-app:prod
Be sure to make it public. To see your packages and make this change, visit
Next, use the Create Project command in the Section Console in order to deploy your new container. Use the image name
ghcr.io/YOUR_GITHUB_USERNAME/my-django-app:prod with port 8080.
See the pods running on Section's network with either the Kubernetes dashboard or
kubectl get pods -o wide. The
-o wide switch shows where your app is running according to the default AEE location optimization strategy. Your app will be optimally deployed according to traffic. In lieu of significant traffic, your deployment will be made to default locations.
Finally, follow the instructions that configure DNS and TLS.
See What You've Built
See the Python Django app you've built by visiting the
YOUR.DOMAIN.COM according to your DNS and HTTPS configuration.